Displaying japanese characters in visual c++

霸气de小男生 提交于 2019-12-11 15:06:22

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!