如何在Java的RandomAccessFile中正确从文件中读取字节?

huangapple go评论95阅读模式
英文:

How to read byte correctly from file using Java RandomAccessFile?

问题

我有一个包含字节88(即十进制136)的二进制文件。

我正在使用Java的RandomAccessFile类和“read”方法通过预定义的缓冲区读取此字节。我还尝试过“readUnsignedByte()”,但出现了与下面相同的问题。

RandomAccessFile randomAccessFile = new RandomAccessFile(inputFile, "r");
byte[] headerBuffer = new byte[32];
randomAccessFile.read(headerBuffer);

上面的代码将88转换为-120,而不是136。

我意识到高位比特或其他设置了,但我仍然需要能够读取文件并获取88或136。

问题在于这个字节是二进制文件中的偏移量,我可以在其中找到第一个记录,因此负数行不通。

希望能得到建议。

谢谢,

英文:

I have a binary file with the byte 88 (which is 136 decimal).

I'm using Java's RandomAccessFile class and the "read" method to read this byte via a pre-defined buffer. I also tried "readUnsignedByte()" but get the same issue as below.

RandomAccessFile randomAccessFile = new RandomAccessFile(inputFile, "r");
byte[] headerBuffer = new byte[32];
randomAccessFile.read(headerBuffer);

The code above gives me -120 for 88, not 136.

I realize that the high-order bit or whatever is set, but I still need to be able to read the file and either get 88 or 136.

The issue is that this byte is the offset into the binary file where I can find the first record so a negative number won't work.

Would appreciate suggestions.

Thanks,

答案1

得分: 1

只需执行 b & 0xFF 即可将其转换为正整数。

英文:

Just do b & 0xFF to convert it to positive integer.

答案2

得分: 1

你可以使用 Byte.toUnsignedInt 方法以“无符号”的方式将一个 byte 转换为一个 int

jshell> byte b = (byte) 0x88;
b ==> -120
jshell> Byte.toUnsignedInt(b);
$57 ==> 136

这个操作与 b & 0xff 是相同的,但不需要涉及到魔术数字或位运算的知识。

英文:

You can use the Byte.toUnsignedInt method to convert a byte into an int in an "unsigned" way.

jshell> byte b = (byte) 0x88;
b ==> -120
jshell> Byte.toUnsignedInt(b);
$57 ==> 136

The operation is the same as b & 0xff but doesn't invoke magic numbers or knowledge about bitwise operators.

答案3

得分: 0

问题:批量读取存在问题

你所使用的 read(byte[]) 方法用于一次读取多个字节,实际上读取的是一个有符号字节,就像 readByte() 那样。因此,你是对的,第一位代表了符号,在Java中可能会被解释为负数。

使用偏移量并读取无符号字节

随机访问的好处是可以使用 seek(long) 跳转到文件中的位置/偏移量。
因此,如果你知道记录指针字节的确切位置,你可以跳转到那里,并使用 readUnsignedByte() 继续读取所需的字节,作为无符号字节

另请参阅

https://stackoverflow.com/questions/5144080/how-can-i-read-a-file-as-unsigned-bytes-in-java

英文:

Issue with bulk-read

The method read(byte[]) you use for reading multiple bytes at-once actually reads a signed byte as in readByte(). Thus you're right, the first bit represents the sign and will thus be interpreted by Java potentially leading to negative number.

Use offset and read unsigned

Benefit of random-access is to jump to the position/offset in file using seek(long).
So, if you know the exact position of that record-pointer byte, you can jump there and proceed reading the desired byte as unsigned using readUnsignedByte().

See also

https://stackoverflow.com/questions/5144080/how-can-i-read-a-file-as-unsigned-bytes-in-java

huangapple
  • 本文由 发表于 2020年4月8日 03:06:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/61087551.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定