与问题字节数组到Int Array有关,但是我想将每个字节转换为整数,而不是每个4字节。
有没有比这更好/更清洁的方法:
protected static int[] bufferToIntArray(ByteBuffer buffer) {
byte[] byteArray = new byte[buffer.capacity()];
buffer.get(byteArray);
int[] intArray = new int[byteArray.length];
for (int i = 0; i < byteArray.length; i++) {
intArray[i] = byteArray[i];
}
return intArray;
}
我可能更喜欢
int[] array = new int[buffer.capacity()];
for (int i = 0; i < array.length; i++) {
array[i] = buffer.get(i);
}
return array;
for kotlin 程序员。
fun getIntArray(byteBuffer: ByteBuffer): IntArray{
val array = IntArray(byteBuffer.capacity())
for (i in array.indices) {
array[i] = byteBuffer.getInt(i)
}
return array
}
这将生成一个 int 数组:
int[] intArray = byteBuffer.asInBuffer().array()