繁体   English   中英

fastcgipp <utf8字符无输出

[英]fastcgipp < no output for utf8 characters

编辑

我在这里通过键入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-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是合理的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM