问题
Does anyone here have an idea how to work with japanese character in visual c++?
I'm trying to display a Japanese name in console with visual c++.
#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;
int main()
{
cout << "北島 美奈" << endl;
return 0;
}
Output in the console:
?? ??
Press any key to continue ...
Hope someone can help. Thank you.
回答1:
I've tested with my own code both UTF-8 and EUC-KR(korean) on a console window using a cmd.exe.
This is my source code.
#include <string>
#include <iostream>
#include <windows.h>
int main()
{
int codepage = CP_ACP; //CP_ACP, CP_OEMCP
int conv_codepage = CP_UTF8; //CP_UTF8
char str[256];
char str1[256];
wchar_t tstr[256], tstr2[256];
memset(str, 0x00, 256);
memset(str1, 0x00, 256);
memset(tstr, 0x00, 256);
memset(tstr2, 0x00, 256);
memcpy(str, " 北島 美奈", sizeof(str));
int nLen = MultiByteToWideChar(codepage, 0, str, -1, 0, 0);
MultiByteToWideChar(codepage, 0, str, -1, tstr, nLen);
int len = WideCharToMultiByte( conv_codepage, 0, tstr, -1, NULL, 0, 0, 0 );
WideCharToMultiByte(conv_codepage, 0, tstr, -1, str1, len ,0 ,0);
cout << "2... " << str1 << endl;
return 0;
}
case 1 UTF-8: the result on a console The output is reasonable because the str1 variable is an utf-8 string. I've got a correct utf-8 on a utf-8 console window.
case 2 EUC-KR: the result on a console I think this case is also acceptable utf-8 string with a utf-8 string.
Then changing the code as follows
cout << "2... " << str << endl;
to
cout << "2... " << str1 << endl;
case 1 UTF-8: the result on a console I think this is okey to me for an unicode string on a utf-8 console.
case 1 EUC-KR: the result on a console
It is still correct unicode string in a euc-kr codepage.
来源:https://stackoverflow.com/questions/50404388/displaying-japanese-characters-in-visual-c