Visual C ++のUnicodeリテラル
-
26-12-2019 - |
質問
次のコードを考慮してください。
#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();
}
.
GCC 4.8 Linux(Ubuntu 14.04)では、ファイルtest.txt
にはこれが含まれています。
6:€€
.
Windows上のVisual C ++ 2013では、これが含まれています。
4:€\x80
.
( '\ x80' by 'i:1つの8ビット文字0x80)を意味します。
€
を使用してstd::wstring
文字を出力するようにコンパイラを完全に取得できませんでした。
2つの質問:
- Microsoftコンパイラは、
char*
リテラルで実行されているとどのようなことを正確に考えていますか?それは明らかにそれをエンコードする何かをしていますが、明確なものは何ですか。 -
std::wstring
文字を出力するようにstd::wofstream
と€
を使用して上記のコードを書き換える正しい方法は何ですか?
解決
これは、ASCII文字列のUnicode文字リテラルである\u20ac
を使用しているためです。
MSVCは、4つの狭い文字である"\xe2\x82\xac\u20ac"
として0xe2, 0x82, 0xac, 0x80,
をエンコードします。それはEURO文字を標準にマッピングする
gccは、Unicodeリテラル\u20ac
を3バイトのUTF-8シーケンス/u20ac
に変換しているため、結果の文字列は0xe2, 0x82, 0xac
として終了します。
#include <string>
#include <fstream>
#include <iomanip>
#include <codecvt>
int main()
{
std::wstring s = L"\u20ac\u20ac";
std::wofstream out("test.txt");
std::locale loc(std::locale::classic(), new std::codecvt_utf8<wchar_t>);
out.imbue(loc);
out << s.length() << L":" << s << std::endl;
out << std::endl;
out.close();
}
.
#include <string>
#include <fstream>
#include <iomanip>
#include <codecvt>
int main()
{
std::wstring s = L"\u20ac\u20ac";
std::wofstream out("test.txt", std::ios::binary );
std::locale loc(std::locale::classic(), new std::codecvt_utf16<wchar_t, 0x10ffff, std::little_endian>);
out.imbue(loc);
out << s.length() << L":" << s << L"\r\n";
out << L"\r\n";
out.close();
}
.
所属していません StackOverflow