我找到了this解决方案,但它似乎适用于 Java SE。我找不到 System.out.format() 函数的替代方法。另外,我已将 ByteBuffer.allocate()
函数更改为 ByteBuffer.allocateDirect()
这是正确的吗?
byte[] bytes = ByteBuffer.allocate(4).putInt(1695609641).array();
for (byte b : bytes) {
System.out.format("0x%x ", b);
}
谢谢。
最佳答案
如果你想要network byte order又名 big-endian 顺序,在 Java 的序列化和远程处理库中使用:
static byte[] intToBytesBigEndian(int i) {
return new byte[] {
(byte) ((i >>> 24) & 0xff),
(byte) ((i >>> 16) & 0xff),
(byte) ((i >>> 8) & 0xff),
(byte) (i & 0xff),
};
}
关于Java 整数到字节数组 Blackberry J2ME,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9587605/