package com.huimv.admin.jinghongtimer;

public class HexUtils {
    
    // 16进制字符串转字节数组（例如："A1B2" → byte[]{0xA1, 0xB2}）
    public static byte[] hexStringToBytes(String hex) {
        hex = hex.replaceAll("\\s", ""); // 移除空格
        if (hex.length() % 2 != 0) {
            throw new IllegalArgumentException("Hex string length must be even");
        }
        byte[] bytes = new byte[hex.length() / 2];
        for (int i = 0; i < hex.length(); i += 2) {
            String byteStr = hex.substring(i, i + 2);
            bytes[i/2] = (byte) Integer.parseInt(byteStr, 16);
        }
        return bytes;
    }

    // 字节数组转16进制字符串（带空格分隔）
    public static String bytesToHexString(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        for (byte b : bytes) {
            sb.append(String.format("%02X ", b));
        }
        return sb.toString().trim();
    }
}