(转载)http://stackoverflow.com/questions/2183240/java-integer-to-byte-array
方案(1):
byte[] bytes = ByteBuffer.allocate(Integer.SIZE / Byte.SIZE)
.putInt(1695609641).array();
for (byte b : bytes) {
<span style="white-space:pre"> </span>System.out.format("0x%02x ", b);
}
方案(2):
byte[] IntToByteArray( int data ) {
byte[] result = new byte[4];
result[0] = (byte) ((data & 0xFF000000) >> 24);
result[1] = (byte) ((data & 0x00FF0000) >> 16);
result[2] = (byte) ((data & 0x0000FF00) >> 8);
result[3] = (byte) ((data & 0x000000FF) >> 0);
return result;
}
//--------------------------------------------------------------------------------------------------------------
总结:可以看到这里还是使用两种方式实现了。
这里注意格式0x%02x和0x%02X区别。一个是小写,一个是大写。02表示不足两位补0。