package com.hboxs.serialport.util;

public class HexUtils {
    /**
     * 16进制字符串转2进制字符串
     *
     * @param hexStr 16进制字符串
     * @return 2进制字符串
     */
    public static String hexStr2BinStr(String hexStr) {
        // 16进制转2进制
        StringBuilder binDataStrBuilder = new StringBuilder();
        for (int i = 0; i < hexStr.length(); i++) {
            String hex = Integer.toBinaryString(Integer.parseInt(hexStr.charAt(i) + "", 16));
            if (hex.length() == 1) {
                binDataStrBuilder.append("000");
            } else if (hex.length() == 2) {
                binDataStrBuilder.append("00");
            } else if (hex.length() == 3) {
                binDataStrBuilder.append("0");
            }
            binDataStrBuilder.append(hex);
        }

        return binDataStrBuilder.toString();
    }
}
