【发布时间】:2021-11-06 12:40:38
【问题描述】:
我想连接两个服务器应用程序。一种是用 Java 编写的,另一种是用 Go 编写的。两者都通过字节级别的简单协议进行通信。
在 Go 应用中我得到了这个结果:
buf := bytes.NewBuffer(make([]byte, 0, 17)
binary.Write(buf, binary.LittleEndian, 1066249)
0 = {uint8} 79
1 = {uint8} 74
2 = {uint8} 16
3 = {uint8} 0
但如果我在我的 Java 应用程序中做同样的事情,我会得到这个数字:
byte[] result = ByteBuffer.allocate(Integer.SIZE / Byte.SIZE).order(ByteOrder.LITTLE_ENDIAN).putInt(1066249).array();
0 = 9
1 = 69
2 = 16
3 = 0
有人知道,我在 Java 方面做错了什么吗?
【问题讨论】:
-
Java 结果是正确的。 1066249 是 0x00104509,在 little-endian 中是 0x09 0x45 0x10 0x00,或 9 69 16 0。