Java–如何连接和分割字节数组byte[]在此⽰例中,我们将向您展⽰如何使⽤ByteBuffer和System.arraycopy连接和拆分字节数组。
ByteBuffer
public static byte[] joinByteArray(byte[] byte1, byte[] byte2) {
return ByteBuffer.allocate(byte1.length + byte2.length)
.put(byte1)
.put(byte2)
.array();
}
public static void splitByteArray(byte[] input) {
ByteBuffer bb = ByteBuffer.wrap(input);
byte[] cipher = new byte[8];
byte[] nonce = new byte[4];
byte[] extra = new byte[2];
<(cipher, 0, cipher.length);
<(nonce, 0, nonce.length);
<(extra, 0, extra.length);
}
System.arraycopy
public static byte[] joinByteArray(byte[] byte1, byte[] byte2) {
byte[] result = new byte[byte1.length + byte2.length];
System.arraycopy(byte1, 0, result, 0, byte1.length);
System.arraycopy(byte2, 0, result, byte1.length, byte2.length);
return result;
}
public static void splitByteArray(byte[] input) {
byte[] cipher = new byte[8];
byte[] nonce = new byte[4];
byte[] extra = new byte[2];
System.arraycopy(input, 0, cipher, 0, cipher.length);
System.arraycopy(input, cipher.length, nonce, 0, nonce.length);
System.arraycopy(input, cipher.length + nonce.length, extra, 0, extra.length);
}
1.连接字节数组
此Java⽰例使⽤ByteBuffer或System.arraycopy来连接或连接两个字节数组。
JoinByteArrayExample.java
package com.mkyong.nio;
import java.nio.ByteBuffer;
public class JoinByteArrayExample {
public static void main(String[] args) {
String str1 = "Hello World ";
String str2 = "Java";
byte[] bytes = Bytes(), Bytes());
byte[] bytes2 = Bytes(), Bytes());
// byte[] to String
System.out.println("Result      : " + new String(bytes));
System.out.println("Result (Hex) : " + convertBytesToHex(bytes));
System.out.println("Result2      : " + new String(bytes2));
System.out.println("Result2 (Hex): " + convertBytesToHex(bytes2));    }
public static byte[] joinByteArray(byte[] byte1, byte[] byte2) {
return ByteBuffer.allocate(byte1.length + byte2.length)
.put(byte1)
.put(byte2)
.array();
}
public static byte[] joinByteArray2(byte[] byte1, byte[] byte2) {
byte[] result = new byte[byte1.length + byte2.length];
System.arraycopy(byte1, 0, result, 0, byte1.length);
System.arraycopy(byte2, 0, result, byte1.length, byte2.length);
return result;
}
public static String convertBytesToHex(byte[] bytes) {
StringBuilder result = new StringBuilder();
for (byte temp : bytes) {
result.append(String.format("%02x", temp));
}
String();
}
}
输出量
Terminal
Result      : Hello World Java
Result (Hex) : 48656c6c6f20576f726c64204a617661
Result2      : Hello World Java
Result2 (Hex): 48656c6c6f20576f726c64204a617661
2.分割字节数组
在Java中,我们可以使⽤ByteBuffer或System.arraycopy将单个字节数组拆分为多个字节数组。
例如,此000102030a0b0c0d1a1b1c1d2f2f以⼗六进制表⽰000102030a0b0c0d1a1b1c1d2f2f是⼀个字节数组(14个字节),它是密码(8个字节)+随机数(4个字节)+额外(2个字节)的组合。
000102030a0b0c0d | 1a1b1c1d | 2f2f
cipher | nonce | extra
SplitByteArrayExample.java
package com.mkyong.nio;
import java.nio.ByteBuffer;
public class SplitByteArrayExample {
public static void main(String[] args) {
byte[] input = {0x00, 0x01, 0x02, 0x03, 0x0a, 0x0b, 0x0c, 0x0d, 0x1a, 0x1b, 0x1c, 0x1d, 0x2f, 0x2f};
if (input.length != 14) {
throw new IllegalArgumentException("input must be 14 bytes.");java中split的用法
}
ByteBuffer bb = ByteBuffer.wrap(input);
byte[] cipher = new byte[8];
byte[] nonce = new byte[4];
byte[] extra = new byte[2];
<(cipher, 0, cipher.length);
<(nonce, 0, nonce.length);
<(extra, 0, extra.length);
System.out.println("Input (Hex)    : " + convertBytesToHex(input));
System.out.println("\n--- ByteBuffer ---");
System.out.println("Cipher(Hex)    : " + convertBytesToHex(cipher));
System.out.println("Nonce (Hex)    : " + convertBytesToHex(nonce));
System.out.println("Nonce (Hex)    : " + convertBytesToHex(extra));
byte[] cipher2 = new byte[8];
byte[] nonce2 = new byte[4];
byte[] extra2 = new byte[2];
System.arraycopy(input, 0, cipher2, 0, cipher2.length);
System.arraycopy(input, cipher2.length, nonce2, 0, nonce2.length);
System.arraycopy(input, cipher2.length + nonce2.length, extra2, 0, extra2.length);
System.out.println("\n--- System.arraycopy ---");
System.out.println("Cipher2 (Hex)  : " + convertBytesToHex(cipher2));
System.out.println("Nonce2  (Hex)  : " + convertBytesToHex(nonce2));
System.out.println("Nonce2  (Hex)  : " + convertBytesToHex(extra2));
}
public static String convertBytesToHex(byte[] bytes) {
StringBuilder result = new StringBuilder();
for (byte temp : bytes) {
result.append(String.format("%02x", temp));
}
String();
}
}
输出量
Terminal
Input (Hex)    : 000102030a0b0c0d1a1b1c1d2f2f
--- ByteBuffer ---
Cipher(Hex)    : 000102030a0b0c0d
Nonce (Hex)    : 1a1b1c1d
Nonce (Hex)    : 2f2f
--- System.arraycopy ---
Cipher2 (Hex)  : 000102030a0b0c0d
Nonce2  (Hex)  : 1a1b1c1d
Nonce2  (Hex)  : 2f2f
参考⽂献

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。