Reputation: 4152
Context: Japanese sample data, does not get displayed (cout) as is taken in sample. Why so?
Below is the code:
std::setlocale(LC_ALL,"");
wchar_t *pStrAddr = L"日本語";
wcout <<"pStrAddr:: "<< pStrAddr << std::endl;
Output is observed as:
pStrAddr:: �,�
Upvotes: 1
Views: 2752
Reputation: 4152
Please see the code below for answers, it works well now.
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");
Also I had to use the wcstombs together with the setlocale. Here goes my revised code that works.
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';
Output::
pStrAddr:: �,�
HARDCODED : 日本語
multibyte strBuffer: 日本語
Upvotes: 1