问题写入0xFF到文件 [英] Problem writing 0xFF to file
问题描述
我正在尝试使用PrintStream在java中写入0xFF文件。当我用Hex编辑器打开它时,其他值正确写入文件,但是假设显示0xFF的值改为0xC3BF。
I'm trying to write a 0xFF to file in java using PrintStream. Other values are written to the file correctly when I open it with a Hex editor but the value that is suppose to show 0xFF got 0xC3BF instead.
使用的变量类型是int 。经过几次尝试后,我还发现我可以放入的最大值是0x7F,这将在十六进制编辑器中正确显示,如果我输入0x80,那么十六进制编辑器将显示0xC280。
The variable type used is int. After a few tried I also discover that the "maximum" value i can put is 0x7F this will be displayed correctly in the Hex editor, if i put 0x80 then the hex editor will display 0xC280.
出了什么问题?
推荐答案
稍微调查显示输出的值是UTF-8编码的(以下行)是Python):
A little investigation shows that the outputted values are UTF-8 encoded (the following lines are Python):
In [1]: "\xc2\x80".decode("utf-8")
Out[1]: u'\x80'
In [2]: "\xc3\xbf".decode("utf-8")
Out[2]: u'\xff'
确保为<$ c选择正确的重载$ c> print / println
/ 写
以便将整数值写为字节而不是字符或字符串。请参阅所有人的 PrintStream文档超载。
Make sure you choose the correct overload for print
/println
/write
in order to write the integer value as a byte instead of as character or string. See the documentation of PrintStream for all overloads.
这篇关于问题写入0xFF到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!