Java代码如何实现连接和分割字节数组呢?
下文笔者讲述使用java代码实现连接和分割字节数组,如下所示
实现思路: 使用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]; bb.get(cipher, 0, cipher.length); bb.get(nonce, 0, nonce.length); bb.get(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); }
连接字节数组的示例
package com.java265.nio; import java.nio.ByteBuffer; public class JoinByteArrayExample { public static void main(String[] args) { String str1 = "java265.com"; String str2 = "java爱好者"; byte[] bytes = joinByteArray(str1.getBytes(), str2.getBytes()); byte[] bytes2 = joinByteArray2(str1.getBytes(), str2.getBytes()); // 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)); } return result.toString(); } }
分割字节数组
package com.java265.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."); } ByteBuffer bb = ByteBuffer.wrap(input); byte[] cipher = new byte[8]; byte[] nonce = new byte[4]; byte[] extra = new byte[2]; bb.get(cipher, 0, cipher.length); bb.get(nonce, 0, nonce.length); bb.get(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)); } return result.toString(); } }
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。