Writing utf16 to file in binary mode

前端 未结 6 1132
逝去的感伤
逝去的感伤 2020-11-27 04:44

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(\"tes         


        
6条回答
  •  醉话见心
    2020-11-27 05:36

    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.

提交回复
热议问题