【发布时间】:2014-09-24 04:26:51
【问题描述】:
考虑以下代码:
#include <string>
#include <fstream>
#include <iomanip>
int main() {
std::string s = "\xe2\x82\xac\u20ac";
std::ofstream out("test.txt");
out << s.length() << ":" << s << std::endl;
out << std::endl;
out.close();
}
在 Linux (Ubuntu 14.04) 上的 GCC 4.8 下,文件 test.txt 包含以下内容:
6:€€
在 Windows 上的 Visual C++ 2013 下,它包含以下内容:
4:€\x80
(“\x80”是指单个 8 位字符 0x80)。
我完全无法让任一编译器使用std::wstring 输出€ 字符。
两个问题:
- Microsoft 编译器究竟认为它对
char*文字做了什么?它显然在做一些事情来编码它,但不清楚。 - 使用
std::wstring和std::wofstream重写上述代码以输出两个€字符的正确方法是什么?
【问题讨论】:
-
L"\x20ac\x20ac" Windows 上 8 位字符串的编码是环境 8 位代码页,在美国是 1252。您正在使用 utf8。 (您还将输出文件解释为 utf8 而不是 1252。)
-
公平一点 - Windows 上的“它包含这个”是根据 Notepad++ 的编码设置为 UTF-8。
-
嗯,systeminfo 将系统和输入本地人都指定为“en-gb;English (United Kingdom)”,想一想这是否是 UTF-8 语言环境,它没有说明。
-
没有 UTF-8 语言环境这样的东西。代码页 65001 (UTF-8) 不能是活动代码页。
-
那么“en_GB.utf8”是什么?
标签: visual-c++ unicode unicode-escapes unicode-literals