可能重复:
Convert integer into byte array (Java)Java - 将int转换为4字节的Byte数组?
我需要存储一个缓冲区的长度,以字节数组4个字节大。
伪代码:
private byte[] convertLengthToByte(byte[] myBuffer)
{
int length = myBuffer.length;
byte[] byteLength = new byte[4];
//here is where I need to convert the int length to a byte array
byteLength = length.toByteArray;
return byteLength;
}
什么是解决这个问题的最好方法?请记住,我必须稍后将该字节数组转换回整数。
看看这个:http://stackoverflow.com/questions/5399798/byte-array-and-int-conversion-in-java – TacB0sS 2012-06-03 13:44:00