我有一个运行以下命令的 c++ linux 应用程序:
int main()
{
using namespace std;
char str[] = "¡Hola!";
wchar_t wstr[50];
size_t rc;
memset(wstr, 0, sizeof(wstr));
rc = mbstowcs(wstr, str, 50);
cout << "mbstowcs results: ";
cout << "rc = " << rc << endl;
cout << "str:" << str << endl;
wcout << L"wstr:" << wstr << endl;
setlocale(LC_CTYPE,"");
iconv_t cd = iconv_open("WCHAR_T", "UTF-8");
cout << "iconv_open errno = "<< errno << endl;
char *s = str;
char *t = (char *)wstr;
size_t s1 = strlen(str);
size_t s2 = 50;
rc = iconv(cd, &s, &s1, &t, &s2);
cout << "iconv results: ";
cout << "rc = " << rc << endl;
cout << "str:" << str << endl;
wcout << L"wstr:" << wstr << endl;
}
我想将一个 UTF-8 字符向量转换为 wstring,但上面的代码返回了这个结果:
mbstowcs results: rc = 18446744073709551615
str:¡Hola!
wstr:
iconv_open errno = 2
iconv results: rc = 0
str:¡Hola!
wstr:�Hola!
iconv 结果将第一个字符转换为另一个字符。
注意:如果我替换 UCS-4 -INTERNAL 中的 WCHAR_T,则 wstr 不包含任何内容。
有什么帮助吗?
谢谢!