可能是重复问题:
将整数转换为字节数组(Java)
我需要将一个缓冲区的长度存储在一个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;
}
如何最好地实现这个功能?要记住我后来必须将该字节数组转换回整数。