gpt4 book ai didi

c++使用iconv从UTF-8转换为wstring

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:01:35 28 4
gpt4 key购买 nike

我有一个运行以下程序的 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 字符 vector 转换为 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 不包含任何内容。

有什么帮助吗?

谢谢!

最佳答案

关于c++使用iconv从UTF-8转换为wstring,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5482888/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com