gpt4 book ai didi

c++ - 我如何提供 "Japanese/Chinese"数据?

转载 作者:太空宇宙 更新时间:2023-11-04 13:24:04 27 4
gpt4 key购买 nike

上下文: 日本样本数据,不会像样本中那样显示 (cout)。为什么会这样?

代码如下:

std::setlocale(LC_ALL,"");
wchar_t *pStrAddr = L"日本語";
wcout <<"pStrAddr:: "<< pStrAddr << std::endl;

观察到的输出为:

pStrAddr:: �,�

最佳答案

请查看下面的代码以获得答案,它现在运行良好。

Locale setting Code 1:

setenv("LANG","en_US.utf8",1);              //"en_GB.utf8" or "ja_JP.utf8" etc.
cout << "GET ENV .... " << getenv("LANG");
setlocale(LC_ALL,"");

Locale setting Code 2:

setlocale(LC_ALL,"en_US.utf8");

我还必须使用 wcstombs连同 setlocale .这是我修改后的有效代码。

setlocale(LC_ALL,"en_US.utf8");
wchar_t *pStrAddr = L"日本語";
wcout <<"pStrAddr:: "<< pStrAddr << std::endl;
cout << "HARDCODED : 日本語" << endl;

char strBuffer[11]; //char []
int retStrAddr = wcstombs ( strBuffer, pStrAddr, sizeof(strBuffer) );
if (retStrAddr==11)
strBuffer[11-1]='\0'; //since, not NULL terminated
//to string, as per converter support
std::cout << "multibyte strBuffer: " << strBuffer << '\n';

输出:

pStrAddr:: �,�
HARDCODED : 日本語
multibyte strBuffer: 日本語

关于c++ - 我如何提供 "Japanese/Chinese"数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33934762/

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