Java代码如何实现连接和分割字节数组呢?

乔欣 Java经验 发布时间:2023-02-03 15:45:30 阅读数:3663 1
下文笔者讲述使用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();
    }
} 
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/JavaJingYan/202302/16754103695637.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者