Problem writing 0xFF to file

I am trying to write 0xFF to a file in java using PrintStream. The other values ​​are written to the file correctly when I open it with a Hex editor, but the value that is supposed to show 0xFF got 0xC3BF instead.

The variable type used is int. After several tries I also found that the "maximum" value I can set is 0x7F, this will display correctly in the Hex editor, if I put 0x80 then the hex editor will display 0xC280.

What's wrong?

0


source to share


2 answers


A little research shows that the outputted values ​​are UTF-8 encoded (following strings: Python):

In [1]: "\xc2\x80".decode("utf-8")
Out[1]: u'\x80'

In [2]: "\xc3\xbf".decode("utf-8")
Out[2]: u'\xff'

      



Make sure you choose the correct overload for print

/ println

/ write

to write the integer value as a byte instead of a character or string. See the PrintStream documentation for all overloads.

+2


source


What is wrong is that you are writing characters and they are encoded as UTF-8. Write bytes instead.



+4


source







All Articles