如何输出UNI code字在C / C ++ [英] How to output unicode characters in C/C++
问题描述
我有在Windows控制台单向允许输出code字符的问题。
我使用Windows XP和code块与12.11的mingw32-G ++编译器。
I have problems with outputing unicode characters in Windows console. I am using Windows XP and Code Blocks 12.11 with mingw32-g++ compiler.
什么是在Windows控制台?
What is the proper way to output unicode characters in Windows console with C or C++?
这是我的C ++ code:
This is my C++ code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << "šđč枊ĐČĆŽ" << endl; // doesn't work
string s = "šđč枊ĐČĆŽ";
cout << s << endl; // doesn't work
return 0;
}
在此先感谢。 :)
Thanks in advance. :)
推荐答案
大多数这些字符需要超过一个字节更多的EN code,但的std :: COUT
的充满目前区域将只输出ASCII字符。出于这个原因,你可能会看到很多在输出流中怪异的符号或问号。你应该灌输的std :: wcout
与使用UTF-8语言环境,因为这些字符不是ASCII支持:
Most of those characters take more than a byte to encode, but std::cout
's currently imbued locale will only output ASCII characters. For that reason you're probably seeing a lot of weird symbols or question marks in the output stream. You should imbue std::wcout
with a locale that uses UTF-8 since these characters are not supported by ASCII:
// <locale> is required for this code.
std::locale::global(std::locale("en_US.utf8"));
std::wcout.imbue(std::locale());
std::wstring s = L"šđč枊ĐČĆŽ";
std::wcout << s;
对于Windows系统,你将需要以下code:
For Windows systems you will need the following code:
#include <iostream>
#include <string>
#include <fcntl.h>
#include <io.h>
int main()
{
_setmode(_fileno(stdout), _O_WTEXT);
std::wstring s = L"šđč枊ĐČĆŽ";
std::wcout << s;
return 0;
}
这篇关于如何输出UNI code字在C / C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!