我通过输入out << L"Swedish: å ä ö Å Ä Ö"
来解决此问题,这是字符串前面的前缀L,在此答案中解释:What exactly is the L prefix in C++?
我现在的问题是,如果这是一个很好的解决方案,或者是否有解决此问题的首选替代方案?
http://www.nongnu.org/fastcgipp/doc/2.1/a00004.html中的以下编辑方法:
bool response()
{
wchar_t russian[]={ 0x041f, 0x0440, 0x0438, 0x0432, 0x0435, 0x0442, 0x0020, 0x043c, 0x0438, 0x0440, 0x0000 };
wchar_t chinese[]={ 0x4e16, 0x754c, 0x60a8, 0x597d, 0x0000 };
wchar_t greek[]={ 0x0393, 0x03b5, 0x03b9, 0x03b1, 0x0020, 0x03c3, 0x03b1, 0x03c2, 0x0020, 0x03ba, 0x03cc, 0x03c3, 0x03bc, 0x03bf, 0x0000 };
wchar_t japanese[]={ 0x4eca, 0x65e5, 0x306f, 0x4e16, 0x754c, 0x0000 };
wchar_t runic[]={ 0x16ba, 0x16d6, 0x16da, 0x16df, 0x0020, 0x16b9, 0x16df, 0x16c9, 0x16da, 0x16de, 0x0000 };
out << "Content-Type: text/html; charset=utf-8\r\n\r\n";
out << "<html><head><meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
out << "<title>fastcgi++: Hello World in UTF-8</title></head><body>";
out << "English: Hello World<br />";
out << "Russian: " << russian << "<br />";
out << "Greek: " << greek << "<br />";
out << "Chinese: " << chinese << "<br />";
out << "Japanese: " << japanese << "<br />";
out << "Runic English?: " << runic << "<br />";
out << "Swedish: å ä ö Å Ä Ö<br />";
out << "</body></html>";
return true;
}
Content-Type: text/html; charset=utf-8
<html><head><meta http-equiv='Content-Type' content='text/html; charset=utf-8' /><title>fastcgi++: Hello World in UTF-8</title></head><body>English: Hello World<br />Russian: Привет мир<br />Greek: Γεια σας κόσμο<br />Chinese: 世界您好<br />Japanese: 今日は世界<br />Runic English?: ᚺᛖᛚᛟ ᚹᛟᛉᛚᛞ<br />Swedish: <br /></body></html>
English: Hello World
Russian: Привет мир
Greek: Γεια σας κόσμο
Chinese: 世界您好
Japanese: 今日は世界
Runic English?: ᚺᛖᛚᛟ ᚹᛟᛉᛚᛞ
Swedish:
如上所示,最后一条瑞典线具有输出“åäöÅÄÖ”的预期行为。然而,出于某种原因,这被替换为空格。必须有一种方法,我不会太明确地输入那封信的unicode十六进制表示。
经过一些谷歌研究后,我尝试在主脚本的开头添加setLocale
但没有成功。
为什么会这样?
如何解决问题,以便能够以上述方式编码时自由使用任何utf8字符?
答案 0 :(得分:1)
这适用于Linux:
#include <iostream>
#include <locale>
bool response()
{
wchar_t russian[]={ 0x041f, 0x0440, 0x0438, 0x0432, 0x0435, 0x0442, 0x0020, 0x043c, 0x0438, 0x0440, 0x0000 };
wchar_t chinese[]={ 0x4e16, 0x754c, 0x60a8, 0x597d, 0x0000 };
wchar_t greek[]={ 0x0393, 0x03b5, 0x03b9, 0x03b1, 0x0020, 0x03c3, 0x03b1, 0x03c2, 0x0020, 0x03ba, 0x03cc, 0x03c3, 0x03bc, 0x03bf, 0x0000 };
wchar_t japanese[]={ 0x4eca, 0x65e5, 0x306f, 0x4e16, 0x754c, 0x0000 };
wchar_t runic[]={ 0x16ba, 0x16d6, 0x16da, 0x16df, 0x0020, 0x16b9, 0x16df, 0x16c9, 0x16da, 0x16de, 0x0000 };
std::wcout << "Content-Type: text/html; charset=utf-8\r\n\r\n" << std::endl;
std::wcout << "<html><head><meta http-equiv='Content-Type' content='text/html; charset=utf-8' />" << std::endl;
std::wcout << "<title>fastcgi++: Hello World in UTF-8</title></head><body>" << std::endl;
std::wcout << "English: Hello World<br />" << std::endl;
std::wcout << "Russian: " << russian << "<br />" << std::endl;
std::wcout << "Greek: " << greek << "<br />" << std::endl;
std::wcout << "Chinese: " << chinese << "<br />" << std::endl;
std::wcout << "Japanese: " << japanese << "<br />" << std::endl;
std::wcout << "Runic English?: " << runic << "<br />" << std::endl;
std::wcout << L"Swedish: å ä ö Å Ä Ö<br />" << std::endl;
std::wcout << "</body></html>" << std::endl;
return true;
}
int main()
{
std::locale::global(std::locale(""));
response();
}
注意(1)输出是一个宽流,(2)瑞典字符串文字是宽(L"whatever"
)。字符串文字前面的L前缀(“Long”)表示文字是宽字符串文字(wchar_t[]
),而不是常规字符串文字(char[]
)。
窄字符串文字在这里不起作用,因为窄字符集默认为UTF-8,默认情况下不存在从UTF-8到任何宽编码的转换(可能是UCS4)。每个字节都被加宽,这是完全错误的。如果你想要你可以自己转换或使用标准转换函数之一:mbstowcs(不是真正的可移植)或C ++ 11 wstring_convert(不使用gcc / libstdc ++,使用clang / libc ++)。
如何在Windows上完成这项工作是任何人的猜测。
建议坚持使用char
和UTF-8,或wchar_t
和UCS4
(在Linux上)。由于您要输出UTF-8,因此使用char
而不是wchar_t
是合理的。