C++ 如何将 wchar_t 值打印到控制台?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2493785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How I can print the wchar_t values to console?
提问by zed91
Example:
例子:
#include <iostream>
using namespace std;
int main()
{
wchar_t en[] = L"Hello";
wchar_t ru[] = L"Привет"; //Russian language
cout << ru
<< endl
<< en;
return 0;
}
This code only prints HEX-values like adress. How to print the wchar_t string?
此代码仅打印十六进制值,如地址。如何打印 wchar_t 字符串?
回答by Nate
Edit: This doesn't work if you are trying to write text that cannot be represented in your default locale. :-(
编辑:如果您尝试编写无法在默认语言环境中表示的文本,这将不起作用。:-(
Use std::wcout
instead of std::cout
.
使用std::wcout
代替std::cout
。
wcout << ru << endl << en;
回答by Konrad
回答by Stevoisiak
回答by Ray Chakrit
Windows has the very confusing information. You should learn C/C++ concept from Unix/Linux before programming in Windows.
Windows 有非常混乱的信息。在 Windows 编程之前,您应该从 Unix/Linux 学习 C/C++ 概念。
wchar_t stores character in UTF-16 which is a fixed 16-bit memory size called wide character but wprintf() or wcout() will never print non-english wide characters correctly because no console will output in UTF-16. Windows will output in current locale while unix/linux will output in UTF-8, all are multi-byte. So you have to convert wide characters to multi-byte before printing. The unix command wcstombs() doesn't work on Windows, use WideCharToMultiByte() instead.
wchar_t 将字符存储在 UTF-16 中,这是一个固定的 16 位内存大小,称为宽字符,但 wprintf() 或 wcout() 永远不会正确打印非英文宽字符,因为没有控制台会以 UTF-16 输出。Windows 将以当前语言环境输出,而 unix/linux 将以 UTF-8 输出,都是多字节的。所以你必须在打印前将宽字符转换为多字节。unix 命令 wcstombs() 在 Windows 上不起作用,请改用 WideCharToMultiByte()。
First you need to convert file to UTF-8 using notepad or other editor. Then install font in command prompt console so that it can read/write in your language and change code page in console to UTF-8 to display correctly by typing in the command prompt "chcp 65001" while cygwin is already default to UTF-8. Here is what I did in Thai.
首先,您需要使用记事本或其他编辑器将文件转换为 UTF-8。然后在命令提示符控制台中安装字体,以便它可以用您的语言读/写并将控制台中的代码页更改为 UTF-8 以通过在命令提示符下键入“chcp 65001”来正确显示,而 cygwin 已经默认为 UTF-8。这是我在泰语中所做的。
#include <windows.h>
#include <stdio.h>
int main()
{
wchar_t* in=L"?????"; // thai language
char* out=(char *)malloc(15);
WideCharToMultiByte(874, 0, in, 15, out, 15, NULL, NULL);
printf(out); // result is correctly in Thai although not neat
}
Note that 874=(Thai) code page in the operating system, 15=size of string
注意操作系统中的 874=(Thai) 代码页,15=字符串的大小
My suggestion is to avoid printing non-english wide characters to console unless necessary because it is not easy.
我的建议是除非必要,否则避免将非英文宽字符打印到控制台,因为这并不容易。
回答by Michael Speer
You could use use a normal char array that is actually filled with utf-8 characters. This should allow mixing characters across languages.
您可以使用实际填充 utf-8 字符的普通 char 数组。这应该允许跨语言混合字符。
回答by Max Barannyk
#include <iostream>
using namespace std;
void main()
{
setlocale(LC_ALL, "Russian");
cout << "\tДОБРО ПОЖАЛОВАТЬ В КИНО!\n";
}