c++ - fastcgipp < 没有输出 utf8 字符

标签 c++ utf-8 fastcgi++

编辑

我通过输入 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 字符的问题?

最佳答案

这适用于 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_tUCS4(在 Linux 上)。既然要输出UTF-8,那么用char比较合理,不用wchar_t

关于c++ - fastcgipp < 没有输出 utf8 字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27089234/

相关文章:

c++ - 如果复制列表初始化允许显式构造函数会出现什么问题?

c++ - 无法理解指针机制

c++ - websocket++ 使用 fastcgi++ 的 session 示例

apache - 在curl中传递POST数据时请求实体太大

python - 检查解释器是否嵌入

php - 如何在 MySQL 数据库中存储苗族字符

c++ - 从 C++ 文件中读取和打印 UTF-8 符号

r - 最佳实践 : Should I try to change to UTF-8 as locale or is it safe to leave it as is?

boost - 安装FastCGI++

c++ - 我们应该用什么来代替 DCOM 通信?