java 如何拆分byte数组成新的多个byte数组

2024-12-16 18:49:37
推荐回答(2个)
回答1:

public void test() { byte[] content = { 0x00, 0x01, 0x12, 0x34, 0x0E, 0x10, 0x02, 0x00, 0x00, 0x00, 0x01, 0x01, 0x12, 0x34, 0x0E, 0x10, 0x01, 0x00, 0x00, 0x02, 0x02, 0x02, 0x12, 0x34, 0x0E, 0x10, 0x01, 0x00, 0x00, 0x03 }; for (int i = 0; i < 3; i++) {
byte[] data = new byte[10];
for (int j = 0; j < 10; j++) { data[j] = content[i * data.length + j];
} System.out.println(ByteUtilities.asHex(data)); }
}

public void test() { byte[] content = { 0x00, 0x01, 0x12, 0x34, 0x0E, 0x10, 0x02, 0x00, 0x00, 0x00, 0x01, 0x01, 0x12, 0x34, 0x0E, 0x10, 0x01, 0x00, 0x00, 0x02, 0x02, 0x02, 0x12, 0x34, 0x0E, 0x10, 0x01, 0x00, 0x00, 0x03 }; byte[] data = new byte[10]; int temp = 0; for (int i = 0; i < 3; i++) { for (int j = 0; j < 10; j++) { if (temp < 10 && temp != 10) { data[j] = content[j]; } else { data[j] = content[temp]; } temp = temp + 1; } System.out.println(ByteUtilities.asHex(data)); } }

两种方法,自己试试吧

回答2:

先把byte数组输出,再分到各个数组里就行了,