package com.yeechart.dotMatrix.util;

public class OtherUtil {
    /**
     * 字节数组转为16进制字符串
     *
     * @param b     字节数组
     * @return 16进制字符串
     * 2018.5.5
     */
    private final static byte[] hex = "0123456789ABCDEF".getBytes();

    /**
     * long 转 16进制字节数组
     *
     * @param number
     * @return
     */
    public static byte[] longToByteArray(long number, int len) {
        byte[] byteArray = new byte[len];
        for (int i = len - 1; i >= 0; i--) {
            byteArray[i] = (byte) (number & 0xFF);
            number >>>= 8;
        }
        return byteArray;
    }

    /**
     * 将String，转化为高位补0的固定格式
     *
     * @param str   需要格式化的str
     * @param scale 规定的格式(如2位 即2)
     * @return
     */
    public static String strToHighZeroPadding(String str, int scale) {
        int length = str.length();
        if (length > scale) {
//            throw new IllegalArgumentException(
//                    "转换的格式必须小于规定的格式");
            return null;
        }

        if (length < scale) {
            str = "0" + str;
            return strToHighZeroPadding(str, scale);
        }
        return str;//这是等于
    }

    public static String Bytes2HexString(byte[] b) {
        //先把byte[] 转换为buff[]，再把buff[]转换为字符串
        byte[] buff = new byte[2 * b.length];
        for (int i = 0; i < b.length; i++) {
            buff[2 * i] = hex[(b[i] >> 4) & 0x0f];
            buff[2 * i + 1] = hex[b[i] & 0x0f];
        }
        String a = new String(buff);
        return new String(buff);
    }

}
