0
4

1 回答 1

3

Your text file is probably encoded as UTF16.

The two bytes at the front are the Byte Order Mark (BOM) and aren't part of the text.

You must not alter them. So what you should do is skip the first two bytes and reverse the remainder of the bytes.

But that will also give you problems because you can't just reverse the bytes in a UTF16 code - it will give you the code for a completely different character, or indeed an invalid code.

Anyway, what's happening when you reverse the order is that you wind up with the BOM stuck at the end where it forms an invalid UTF16 code that happens to be the "?" character at the end that you're seeing, and also it messes up the encoding for all the other characters.

However, it looks like Notepad is opening the file using an ANSI encoding, probably for the code page used by your current locale.

The text file contains bytes 0 53 0 52 0 51 0 50 0 49 254 255 and Notepad is converting 0 to a space, and the other values less than 0x80 are being converted to ASCII characters, while 254 is converted to ю and 255 to я (which I assume is the value of those characters in the ANSI code page for your current locale).

I'm guessing you're in a Slavic region which uses Cyrillic script.

于 2013-05-15T10:36:47.183 回答