fastcgipp < utf8 字符没有输出

fastcgipp < no output for utf8 characters

本文关键字:输出 字符 utf8 lt fastcgipp      更新时间:2023-10-16

>Edit

我通过键入 out << L"Swedish: å ä ö Å Ä Ö" 解决了这个问题,即字符串前的前缀 L,在此答案中解释:C++ 中的 L 前缀到底是什么?我现在的问题是,这是否是一个很好的解决方案,或者是否有解决此问题的首选替代方案?


代码

以下编辑的方法来自 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-8rnrn";
       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-8rnrn" << 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