ByteUtils.java 3.77 KB
package com.sunvote.xpadapi.util;

public class ByteUtils {

    private ByteUtils() {
    }

    public static String bytesToHexString(byte[] src, int length) {
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length && i < length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
            stringBuilder.append(" ");
        }
        return stringBuilder.toString();
    }

    public static String bytesToHexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
            stringBuilder.append(" ");
        }
        return stringBuilder.toString();
    }

    public static int findBytes(byte[] source,byte[] find,int index){
        int flag = -1;
        for (int j = index; j < source.length; j++) {
            if (source.length >= j + find.length) {
                if (compareByte(source, j, find)) {
                    flag = j;
                    break;
                }
            }
        }
        return flag;
    }

    public static int byte1ToInt(byte b) {
        int ret = 0;
        ret += (b & 0x000000FF);
        return ret;
    }

    private static boolean compareByte(byte[] source, int start, byte[] find) {
        if (source.length >= start + find.length) {
            for (int i = 0; i < find.length; i++) {
                if (source[start + i] != find[i]) {
                    return false;
                }
            }
            return true;
        }
        return false;
    }

    /**
     * Convert hex string to byte[]
     *
     * @param hexString the hex string
     * @return byte[]
     */
    public static byte[] hexStringToBytes(String hexString) {
        if (hexString == null || hexString.equals("")) {
            return null;
        }
        hexString = hexString.toUpperCase();
        int length = hexString.length() / 2;
        char[] hexChars = hexString.toCharArray();
        byte[] d = new byte[length];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
        }
        return d;
    }

    /**
     * Convert char to byte
     *
     * @param c char
     * @return byte
     */
    private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }


    public static byte[] intToBytes(int value) {
        byte[] src = new byte[4];
        src[3] = (byte) ((value >> 24) & 0xFF);
        src[2] = (byte) ((value >> 16) & 0xFF);
        src[1] = (byte) ((value >> 8) & 0xFF);
        src[0] = (byte) (value & 0xFF);
        return src;
    }

    public static byte[] int2Bytes(int value) {
        byte[] src = new byte[2];
        src[0] = (byte) ((value >> 8) & 0xFF);
        src[1] = (byte) (value & 0xFF);
        return src;
    }

    public static int bytes2Int(byte[] src) {
        int ret = ((src[1] & 0xFF) | ((src[0] & 0xFF) << 8));
        return ret;
    }

    public static String getKeySn(byte[] data) {
        String sn = "";
        String CS = "0123456789ABCDEF";
        for (int i = 0; i < 6; i++) {
            sn += CS.charAt((data[i] >> 4) & 0xF);
            sn += CS.charAt((data[i] >> 0) & 0xF);
        }
        return sn;

    }
}