我想用二进制模式写入一个wstring文件与streamstream,但我认为我做错了。这是我试过的:
ofstream outFile(test.txt,std :: ios :: out | std :: ios :: binary); wstring hello = Lhello; outFile.write((char *)hello.c_str(),hello.length()* sizeof(wchar_t)); outFile.close();在例如Firefox中打开test.txt,编码设置为UTF16,它将显示为:
h e l l o
有人能告诉我为什么会发生这种情况吗?
在十六进制编辑器中打开文件我得到: / p> FF FE 68 00 00 00 65 00 00 00 6C 00 00 00 6C 00 00 00 6F 00 00 00看起来像是因为某些原因,每个字符之间有两个额外的字节?
解决方案我怀疑sizeof(wchar_t)是4在你的环境 - 即它写出UTF-32 / UCS-4而不是UTF-16。这确实是十六进制转码的样子。
这很容易测试(只打印出sizeof(wchar_t)),但我很确定它是怎么回事。 / p>
要从UTF-32 wstring到UTF-16,您需要应用正确的编码,因为代理对会发挥作用。
I'm trying to write a wstring to file with ofstream in binary mode, but I think I'm doing something wrong. This is what I've tried:
ofstream outFile("test.txt", std::ios::out | std::ios::binary); wstring hello = L"hello"; outFile.write((char *) hello.c_str(), hello.length() * sizeof(wchar_t)); outFile.close();Opening test.txt in for example Firefox with encoding set to UTF16 it will show as:
h�e�l�l�o�
Could anyone tell me why this happens?
EDIT:
Opening the file in a hex editor I get:
FF FE 68 00 00 00 65 00 00 00 6C 00 00 00 6C 00 00 00 6F 00 00 00Looks like I get two extra bytes in between every character for some reason?
解决方案I suspect that sizeof(wchar_t) is 4 in your environment - i.e. it's writing out UTF-32/UCS-4 instead of UTF-16. That's certainly what the hex dump looks like.
That's easy enough to test (just print out sizeof(wchar_t)) but I'm pretty sure it's what's going on.
To go from a UTF-32 wstring to UTF-16 you'll need to apply a proper encoding, as surrogate pairs come into play.
更多推荐
以二进制模式将utf16写入文件
发布评论