Java整数到字节数组

斯蒂芬:

我得到一个整数: 1695609641

当我使用方法:

String hex = Integer.toHexString(1695609641);
system.out.println(hex); 

给出:

6510f329

但我想要一个字节数组:

byte[] bytearray = new byte[] { (byte) 0x65, (byte)0x10, (byte)0xf3, (byte)0x29};

我该怎么做?

dfa:

使用Java NIO的ByteBuffer非常简单:

byte[] bytes = ByteBuffer.allocate(4).putInt(1695609641).array();

for (byte b : bytes) {
   System.out.format("0x%x ", b);
}

输出:

0x65 0x10 0xf3 0x29 

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章