swprintf失败与unicode字符在xcode,但工作在visual studio [英] swprintf fails with unicode characters in xcode, but works in visual studio

查看:419
本文介绍了swprintf失败与unicode字符在xcode,但工作在visual studio的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试转换一些现有代码以支持unicode字符时,此问题弹出。如果我试图传递一个unicode字符(在这种情况下im使用欧元符号)到任何* wprintf函数,它将失败,但似乎只在xcode。同样的代码在视觉工作室很好,我甚至能够得到一个朋友来测试它成功与gcc在linux。这里是冒犯的代码:

  wchar_t _teststring [10] = L 
int _iRetVal = swprintf(_teststring,10,LA¥€);

wprintf(Lreturn:%d\\\
,_iRetVal);

//打印存储在字符串中的值以检查是否有任何损坏
for(int i = 0; i wprintf L%d:(%d)\\\
,i,_teststring [i]);
}

在xcode中,对swprintf的调用将返回-1,将成功并继续为3个字符(65,165,8364)中的每个字符打印正确的值。



我已经搜索了很长的解决方案,一个建议已出现多次正在使用呼叫如:

  setlocale(LC_CTYPE,UTF-8); 

我尝试过这个函数的各种参数组合没有成功,进一步调查后,它似乎是如果我尝试将语言环境设置为除默认C之外的任何值,返回null。



我很失望,我可以尝试解决什么这个问题,以及它在其他编译器/平台上的工作只是使它更令人沮丧的事实。任何帮助将非常感激。



编辑:
只是想,我会补充说,当swprintf调用失败时,它设置一个错误代码(92) as:

  #define EILSEQ 92 / *非法字节序列* / 
pre>

解决方案

如果从环境中获取语言环境,它应该可以工作:

  #include< stdio.h> 
#include< wchar.h>
#include< locale.h>

int main(void){
setlocale(LC_ALL,);
wchar_t _teststring [10] = L;
int _iRetVal = swprintf(_teststring,10,LA¥€);

wprintf(Lreturn:%d\\\
,_iRetVal);

//打印存储在字符串中的值以检查是否有任何损坏
for(int i = 0; i wprintf L%d:(%d)\\\
,i,_teststring [i]);
}

}

在我的OS X 10.6使用GCC 4.2.1工作,但是当使用CLang 1.6编译时,它将UTF-8字节放在结果字符串中。



我也可以使用Xcode (使用标准的C ++控制台应用程序模板),但是因为OS X上的图形应用程序没有所需的区域设置环境变量,所以它在Xcode的控制台中不起作用。



您还可以将区域设置为 en_US.UTF-8 setlocale(LC_ALL,en_US.UTF-8)),但这是非便携式的。根据您的目标,可能有更好的替代 wsprintf


While trying to convert some existing code to support unicode characters this problem popped up. If i try to pass a unicode character (in this case im using the euro symbol) into any of the *wprintf functions it will fail, but seemingly only in xcode. The same code works fine in visual studio and I was even able to get a friend to test it successfully with gcc on linux. Here is the offending code:

wchar_t _teststring[10] = L"";
int _iRetVal = swprintf(_teststring, 10, L"A¥€");

wprintf(L"return: %d\n", _iRetVal);

// print values stored in string to check if anything got corrupted
for (int i=0; i<wcslen(_teststring); ++i) {
    wprintf(L"%d: (%d)\n", i, _teststring[i]);
}

In xcode the call to swprintf will return -1, while in visual studio it will succeed and proceed to print out the correct values for each of the 3 chars (65, 165, 8364).

I have googled long and hard for solutions, one suggestion that has appeared a number of times is using a call such as:

setlocale(LC_CTYPE, "UTF-8");

I have tried various combinations of arguments with this function with no success, upon further investigation it appears to be returning null if i try to set the locale to any value other than the default "C".

I'm at a loss as to what else i can try to solve this problem, and the fact it works in other compilers/platforms just makes it all the more frustrating. Any help would be much appreciated!

EDIT: Just thought i would add that when the swprintf call fails it sets an error code (92) which is defined as:

#define EILSEQ      92      /* Illegal byte sequence */

解决方案

It should work if you fetch the locale from the environment:

#include <stdio.h>
#include <wchar.h>
#include <locale.h>

int main(void) {
  setlocale(LC_ALL, "");
wchar_t _teststring[10] = L"";
int _iRetVal = swprintf(_teststring, 10, L"A¥€");

wprintf(L"return: %d\n", _iRetVal);

// print values stored in string to check if anything got corrupted
for (int i=0; i<wcslen(_teststring); ++i) {
    wprintf(L"%d: (%d)\n", i, _teststring[i]);
}

}

On my OS X 10.6, this works as expected with GCC 4.2.1, but when compiled with CLang 1.6, it places the UTF-8 bytes in the result string.

I could also compile this with Xcode (using the standard C++ console application template), but because graphical applications on OS X don't have the required locale environment variables, it doesn't work in Xcode's console. On the other hand, it always works in the Terminal application.

You could also set the locale to en_US.UTF-8 (setlocale(LC_ALL, "en_US.UTF-8")), but that is non-portable. Depending on your goal there may be better alternatives to wsprintf.

这篇关于swprintf失败与unicode字符在xcode,但工作在visual studio的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆