java - 如何将多个字节加在一起?

标签 java byte arrays

我正在尝试编写一个 ROM 文件修补程序,我在文档中找到了这一点:

The next three bytes are the place in the destination file
(the file to be patched) where a change is to be made.

The two bytes following that are the length of the amount
of data to be changed.

如何在 java 中正确地将这三个和两个字节转换为单个数字?

最佳答案

奇怪的是,我看到 Joey 的有效答案,他在 10 分钟前删除了该答案:

(byte1 << 16) + (byte2 << 8) + byte3

我唯一要补充的是:不要将从 InputStream 读取的字节转换为 byte 类型(InputStream#read 返回 int 值0 到 255 之间)。例如,这会将 255 转换为 -1,而我们不需要这样的副作用。

所以,它可能看起来像

(in.read() << 16) + (in.read() << 8) + in.read()

关于java - 如何将多个字节加在一起?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4001614/

相关文章:

java - 在 JSP 中加载页面时填充下拉列表

java - 如何从java中的溢出字节中删除(f)位

go - golang时间对象中有多少字节

c - c 中的字符串作为数组

java - 在 Hibernate 中映射一对多关系?

java - 使用不同对象访问同一类变量的设计

c++ - 骑士之旅将数组传递到链表等等

c - 查找数组中元素地址之间的差异

Java-Azure 列表 Blob 目录

c# - 来自 TransformedBitmap 对象的“跨越”困境