以二进制模式将utf16写入文件

编程入门 行业动态 更新时间:2024-10-21 15:53:35
本文介绍了以二进制模式将utf16写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想用二进制模式写入一个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 00

Looks 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写入文件

本文发布于:2023-10-23 05:50:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1519979.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模式   文件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!