package com.chinaitop.depot.intelligent.utils;

/**
 * @author fly
 * @date 2019��11��15��
 */
public class DataUtils {


    /**
     * * 16���Ʊ�ʾ���ַ���ת��Ϊ�ֽ����� * * @param hexString 16���Ʊ�ʾ���ַ��� * @return byte[] �ֽ�����
     */
    public static byte[] hexStringToByteArray(String hexString) {
        hexString = hexString.replaceAll(" ", "");
        int len = hexString.length();
        byte[] bytes = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            // ��λһ�飬��ʾһ���ֽ�,��������ʾ��16�����ַ�������ԭ��һ���ֽ�
            bytes[i / 2] =
                    (byte)((Character.digit(hexString.charAt(i), 16) << 4) + Character.digit(hexString.charAt(i + 1), 16));
        }
        return bytes;
    }

    /***
     * byte[]����ת��Ϊ16���Ƶ��ַ��� * * @param bytes Ҫת�����ֽ����� * @return ת����Ľ��
     */
    public static String bytesToHexString(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < bytes.length; i++) {
            String hex = Integer.toHexString(0xFF & bytes[i]);
            if (hex.length() == 1) {
                sb.append('0');
            }
            sb.append(hex);
        }
        return sb.toString();
    }



    // �ֽ�ת16
    public static final String byteToString(byte[] bArray) {
        StringBuffer sb = new StringBuffer(bArray.length);
        String sTemp;
        for (int i = 0; i < bArray.length; i++) {

            sTemp = Integer.toHexString(0xFF & bArray[i]);
            if (sTemp.length() < 2)
                sb.append(0);
            sb.append(sTemp.toUpperCase());
        }
        String string = sb.toString();
        return getFileAddSpace(string);
    }

    // 16�ַ����ӿո�
    public static String getFileAddSpace(String replace) {
        String regex = "(.{2})";
        replace = replace.replaceAll(regex, "$1 ");
        return replace;
    }

}
