Java 字节数组/字符串 工具类 BytesUtils
    
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
 
 
public class BytesUtils {
    private BytesUtils() {
    }
    
     
     
     
     
     
     
     
    public static int toBytes(long value, byte[] bytes, int offset) {
        bytes[offset] = (byte) ((value >>> 56) & 0x00FF);
        bytes[offset + 1] = (byte) ((value >>> 48) & 0x00FF);
        bytes[offset + 2] = (byte) ((value >>> 40) & 0x00FF);
        bytes[offset + 3] = (byte) ((value >>> 32) & 0x00FF);
        bytes[offset + 4] = (byte) ((value >>> 24) & 0x00FF);
        bytes[offset + 5] = (byte) ((value >>> 16) & 0x00FF);
        bytes[offset + 6] = (byte) ((value >>> 8) & 0x00FF);
        bytes[offset + 7] = (byte) (value & 0x00FF);
        return 8;
    }
    public static byte[] toBytes(String str) {
        return toBytes(str, StandardCharsets.UTF_8.name());
    }
    public static byte[] toBytes(String str, String charset) {
        if (null == str) {
            return null;
        }
        try {
            return str.getBytes(charset);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }
    public static String bytesToHexString(byte[] src){
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }
    
     
     
     
     
    public static byte[] hexStringToBytes(String hexString) {
        if (hexString == null || hexString.equals("")) {
            return null;
        }
        hexString = hexString.toUpperCase();
        int length = hexString.length() / 2;
        char[] hexChars = hexString.toCharArray();
        byte[] d = new byte[length];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
        }
        return d;
    }
    
     
     
     
     
    private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }
    
     
     
     
     
     
    public static byte[] utf8ToArray(String str) {
        return str.getBytes(StandardCharsets.UTF_8);
    }
    
     
     
     
     
     
    public static String arrayToUtf8(byte[] arr) {
        return new String(arr, StandardCharsets.UTF_8);
    }
}