C++ 宽图表多字节问题
我有我的系统提供的可爱的功能,如果我这样做,这些功能可以很好地工作:C++ 宽图表多字节问题,c++,unicode,utf-8,utf-16,widechar,C++,Unicode,Utf 8,Utf 16,Widechar,我有我的系统提供的可爱的功能,如果我这样做,这些功能可以很好地工作: wstring temp; wcin >> temp; string whatever( toUTF8(getSomeWString()) ); // store whatever, copy, but do not use it as UTF8 (see below) wcout << toUTF16(whatever) << endl; wstring温度; wcin>>温度;
wstring temp;
wcin >> temp;
string whatever( toUTF8(getSomeWString()) );
// store whatever, copy, but do not use it as UTF8 (see below)
wcout << toUTF16(whatever) << endl;
wstring温度;
wcin>>温度;
字符串which(toUTF8(getSomeWString());
//存储任何内容,复制,但不要将其用作UTF8(见下文)
wcout当您将字符串转换为UTF 16时,它是一个16字节宽的字符,您无法将其与ASCII值进行比较,因为它们不是16字节的值。您必须将它们转换为比较,或者将专门的比较写入ASCII函数
我怀疑linux中的UTF8 cout是否会产生相同的正确输出,除非它是常规的ASCII值,因为UTF8和我假设UTF16以类似的方式出现在UTF8之后
好消息是,有很多代码是用来将这些字符串转换为不同的字符集的。当您将字符串转换为UTF 16时,它是一个16字节宽的字符,您无法将其与ASCII值进行比较,因为它们不是16字节的值。您必须将它们转换为比较,或者将专门的比较写入ASCII函数
我怀疑linux中的UTF8 cout是否会产生相同的正确输出,除非它是常规的ASCII值,因为UTF8和我假设UTF16以类似的方式出现在UTF8之后
好消息是,有很多代码是用来将这些字符串转换成不同的字符集的。首先,我要说的是,似乎没有办法通过cout
将UTF-8文本输出到Windows控制台(假设您使用Visual Studio编译)。
但是,对于测试,您可以通过Win32 API fnWriteConsoleA
输出UTF-8文本:
if(!SetConsoleOutputCP(CP_UTF8)) { // 65001
cerr << "Failed to set console output mode!\n";
return 1;
}
HANDLE const consout = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD nNumberOfCharsWritten;
const char* utf8 = "Umlaut AE = \xC3\x84 / ue = \xC3\xBC \n";
if(!WriteConsoleA(consout, utf8, strlen(utf8), &nNumberOfCharsWritten, NULL)) {
DWORD const err = GetLastError();
cerr << "WriteConsole failed with << " << err << "!\n";
return 1;
}
if(!SetConsoleOutputCP(CP_UTF8)){//65001
cerr首先,我要说的是,似乎有一种方法可以通过cout
将UTF-8文本输出到Windows控制台(假设使用Visual Studio编译)。
但是,对于测试,您可以通过Win32 API fnWriteConsoleA
输出UTF-8文本:
if(!SetConsoleOutputCP(CP_UTF8)) { // 65001
cerr << "Failed to set console output mode!\n";
return 1;
}
HANDLE const consout = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD nNumberOfCharsWritten;
const char* utf8 = "Umlaut AE = \xC3\x84 / ue = \xC3\xBC \n";
if(!WriteConsoleA(consout, utf8, strlen(utf8), &nNumberOfCharsWritten, NULL)) {
DWORD const err = GetLastError();
cerr << "WriteConsole failed with << " << err << "!\n";
return 1;
}
if(!SetConsoleOutputCP(CP_UTF8)){//65001
cerr我知道转换(我在前面链接的问题中使用了它们,我转换正是因为我需要执行比较),我试图确定win23 API转换的字符串是否与原始UTF8(linux)相同字符串。linux上的cout
可以很好地输出字符,这就是为什么它首先使用UTF8(嗯,可能还有很多其他原因)。问题是,我不知道┬à┬ç┬é
string也存在于原始UTF8字符串中。我知道转换(我在前面链接的问题中使用了它们,我转换它们正是因为我需要执行比较),我试图确定win23 API转换的字符串是否与原始UTF8(linux)相同字符串。linux上的cout
可以很好地输出字符,这就是为什么它首先使用UTF8(嗯,可能还有很多其他原因)。问题是,我不知道┬à┬ç┬é
字符串也存在于原始UTF8字符串中。如果您仍然使用WriteConsole
,您也可以使用WriteConsole
直接写入UTF-16字符串,从而消除了SetConsoleOutputCP
@Philipp的必要性-是的,首先从utf16转换为UTF8,然后使用WriteConsole几乎没有意义。如果(测试-)中的字符串虽然应用程序已经是utf8,但它可能仍然有意义。如果您使用的是WriteConsole
,您也可以使用WriteConsoleW
直接写入UTF-16字符串,消除了setconsoleoutcp
@Philipp的必要性-是的,首先从utf16转换为utf8,然后使用WriteConsoleA几乎没有意义。如果(test-)应用程序中的字符串已经是utf8,那么它可能仍然有意义。