package com.inspect.base.core.utils;

import java.math.BigInteger;
import java.nio.charset.Charset;

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;

public class HexUtils {
    public static byte[] hexToByteArray(String inHex) {
        int hexlen = inHex.length();
        byte[] result;
        if (hexlen % 2 == 1) {
            ++hexlen;
            result = new byte[hexlen / 2];
            inHex = "0" + inHex;
        } else {
            result = new byte[hexlen / 2];
        }

        int j = 0;

        for (int i = 0; i < hexlen; i += 2) {
            result[j] = hexToByte(inHex.substring(i, i + 2));
            ++j;
        }

        return result;
    }

    public static byte hexToByte(String inHex) {
        return (byte) Integer.parseInt(inHex, 16);
    }

    public static String convertStringToHex(String str) {
        return convertStringToHex(str, "UTF8");
    }

    public static String convertStringToHex(String str, String charsetName) {
        char[] chars = Hex.encodeHex(str.getBytes(Charset.forName(charsetName)));
        return String.valueOf(chars);
    }

    public static String convertHexToString(String hex, String charsetName) {
        String result = "";

        try {
            byte[] bytes = Hex.decodeHex(hex);
            result = new String(bytes, Charset.forName(charsetName));
            return result;
        } catch (DecoderException var4) {
            throw new IllegalArgumentException("Invalid Hex format!");
        }
    }

    public static String convertHexToString(String hex) {
        return convertHexToString(hex, "UTF8");
    }

    public static byte[] int2bytes(int num) {
        return new byte[]{(byte) (num >> 24 & 255), (byte) (num >> 16 & 255), (byte) (num >> 8 & 255), (byte) (num & 255)};
    }

    public static byte[] long2Bytes(long num) {
        byte[] byteNum = new byte[8];

        for (int ix = 0; ix < 8; ++ix) {
            int offset = 64 - (ix + 1) * 8;
            byteNum[ix] = (byte) ((int) (num >> offset & 255L));
        }

        return byteNum;
    }

    public static String binary(byte[] bytes, int radix) {
        String str = (new BigInteger(1, bytes)).toString(radix);
        return str;
    }

    public static byte[] toByteArrayLittle(String hexString) {
        System.out.println("=============toByteArrayLittle:" + hexString);
        if (StringUtils.isEmpty(hexString)) {
            throw new IllegalArgumentException("this hexString must not be empty");
        } else {
            hexString = hexString.toLowerCase();
            byte[] byteArray = new byte[hexString.length() / 2];
            int k = 0;

            for (int i = 0; i < byteArray.length; ++i) {
                byte high = (byte) (Character.digit(hexString.charAt(k), 16) & 255);
                byte low = (byte) (Character.digit(hexString.charAt(k + 1), 16) & 255);
                byteArray[i] = (byte) (low >> 4 | high);
                k += 2;
            }

            return byteArray;
        }
    }

    public static byte[] toByteArray(String hexString) {
        if (StringUtils.isEmpty(hexString)) {
            throw new IllegalArgumentException("this hexString must not be empty");
        } else {
            hexString = hexString.toLowerCase();
            byte[] byteArray = new byte[hexString.length() / 2];
            int k = 0;

            for (int i = 0; i < byteArray.length; ++i) {
                byte high = (byte) (Character.digit(hexString.charAt(k), 16) & 255);
                byte low = (byte) (Character.digit(hexString.charAt(k + 1), 16) & 255);
                byteArray[i] = (byte) (high >> 4 | low);
                k += 2;
            }

            return byteArray;
        }
    }

    public static String numToHex8(int b) {
        return String.format("%02x", new Object[]{Integer.valueOf(b)});
    }

    public static String numToHex16(int b) {
        return String.format("%04x", new Object[]{Integer.valueOf(b)});
    }

    public static String numToHex32(int b) {
        return String.format("%08x", new Object[]{Integer.valueOf(b)});
    }
}
