编辑
我在这里通过键入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: 如上文所示,瑞典的最后一条线有一种预期的输出行为-“\x{e76f}”。然而,由于某种原因,它被替换为空白空间。必须有一种方法,我不需要过多地输入该字母的unicode十六进制表示形式。
在谷歌的一些研究之后,我尝试在主脚本的开头添加setLocale,但没有成功。
这是为什么?
如何解决这个问题,以便在以上述方式编码时能够自由地使用任何utf8字符?
发布于 2014-11-24 11:29:25
这在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)瑞典字符串文本为wide (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是合理的。
https://stackoverflow.com/questions/27089234
复制相似问题