java常用的数据转换工具

public class ByteUtil {
    /**
     * 将多个byte数据,整合到一个数组中
     */
    public static byte[] combineByteArrays(byte[] ... byteArrays){
        int totalCount=0;
        for (byte[] byteArray : byteArrays) {
            totalCount += byteArray.length;
        }
        byte[] result=new byte[totalCount];
        int currentIndex=0;
        for (byte[] byteArray : byteArrays) {
            for (byte b : byteArray) {
                result[currentIndex] = b;
                currentIndex++;
            }
        }
        return result;
    }

    public static byte[] subBytes(byte[] src,int from,int length){
        byte[] result=new byte[length];
        System.arraycopy(src,  from, result, 0, length);
        return result;
    }

    public static int[] bytesToInts(byte[] bytes){
        int[] ints=new int[bytes.length];
        for(int i=0;i<bytes.length;i++){
            ints[i]=(bytes[i]+256)%256;
        }
        return ints;
    }

    /**
     * 字节数组转换成对应的16进制表示的字符串
     */
    public static String bytes2HexStr(byte[] src) {
        StringBuilder builder = new StringBuilder();
        if (src == null || src.length <= 0) {
            return "";
        }
        char[] buffer = new char[2];
        for (byte b : src) {
            buffer[0] = Character.forDigit((b >>> 4) & 0x0F, 16);
            buffer[1] = Character.forDigit(b & 0x0F, 16);
            builder.append(buffer);
        }
        return builder.toString().toUpperCase();
    }

    public static String bytes2HexStr(byte[] src, int dec, int length) {
        byte[] temp = new byte[length];
        System.arraycopy(src, dec, temp, 0, length);
        return bytes2HexStr(temp);
    }

    public static int byteToInt(byte b){
        return (b+256)%256;
    }

    public static int bytesToUnsignedNum(byte[] data,int length){
        int a = 0;
        for(int i = 0;i < length;i++){
            a |= (data[i] & 0xFF) << (i * 8);
        }
        return a;
    }

    public static int bytesToUnsignedNumL2R(byte[] data,int length){
        int a = 0;
        for(int i = 0; i< length;i++){
            a = a<<8;
            a |= (data[i] & 0xFF);
        }
        return a;
    }
    /**
     * 16进制字符串转10进制数字
     */
    public static long hexStr2decimal(String hex) {
        return Long.parseLong(hex, 16);
    }

    /**
     * 把十进制数字转换成足位的十六进制字符串,并补全空位
     */
    public static String decimal2fitHex(long num) {
        String hex = Long.toHexString(num).toUpperCase();
        if (hex.length() % 2 != 0) {
            return "0" + hex;
        }
        return hex.toUpperCase();
    }

    /**
     * 把十进制数字转换成足位的十六进制字符串,并补全空位
     */
    public static String decimal2fitHex(long num, int strLength) {
        String hexStr = decimal2fitHex(num);
        StringBuilder stringBuilder = new StringBuilder(hexStr);
        while (stringBuilder.length() < strLength) {
            stringBuilder.insert(0, '0');
        }
        return stringBuilder.toString();
    }

    public static String fitDecimalStr(int dicimal, int strLength) {
        StringBuilder builder = new StringBuilder(String.valueOf(dicimal));
        while (builder.length() < strLength) {
            builder.insert(0, "0");
        }
        return builder.toString();
    }

    /**
     * 字符串转十六进制字符串
     */
    public static String str2HexString(String str) {
        char[] chars = "0123456789ABCDEF".toCharArray();
        StringBuilder sb = new StringBuilder();
        try {
            byte[] byteArray = str.getBytes(StandardCharsets.UTF_8);
            for (byte b : byteArray) {
                sb.append(chars[(b & 0xf0) >> 4]);
                sb.append(chars[b & 0x0f]);
            }
        }catch (Exception e) {
            e.printStackTrace();
            return null;
        }

        return sb.toString();
    }

    /**
     * 把十六进制表示的字节数组字符串,转换成十六进制字节数组
     */
    public static byte[] hexStr2bytes(String hex) {
        hex=hex.trim().replaceAll(" ","");
        int len = (hex.length() / 2);
        byte[] result = new byte[len];
        char[] achar = hex.toUpperCase().toCharArray();
        for (int i = 0; i < len; i++) {
            int pos = i * 2;
            result[i] = (byte) (hexChar2byte(achar[pos]) << 4 | hexChar2byte(achar[pos + 1]));
        }
        return result;
    }



    /**
     * 把16进制字符[0123456789abcde](含大小写)转成字节
     */
    private static int hexChar2byte(char c) {
        switch (c) {
            case '0':
                return 0;
            case '1':
                return 1;
            case '2':
                return 2;
            case '3':
                return 3;
            case '4':
                return 4;
            case '5':
                return 5;
            case '6':
                return 6;
            case '7':
                return 7;
            case '8':
                return 8;
            case '9':
                return 9;
            case 'a':
            case 'A':
                return 10;
            case 'b':
            case 'B':
                return 11;
            case 'c':
            case 'C':
                return 12;
            case 'd':
            case 'D':
                return 13;
            case 'e':
            case 'E':
                return 14;
            case 'f':
            case 'F':
                return 15;
            default:
                return -1;
        }
    }
}
public class CalculateUtils {
	public static String average(String value1,String value2,String format){
		Double a = Double.parseDouble(StringUtils.isNotBlank(value1)?value1:"0");
		Double b = Double.parseDouble(StringUtils.isNotBlank(value2)?value2:"0");

		if( 0 == a || 0 == b){
			return String.format(StringUtils.isBlank(format)?"%.2f":format,(a + b));
		}else{
			return String.format(StringUtils.isBlank(format)?"%.2f":format,((a + b) / 2.0));
		}
	}

	public static String add(String value1,String value2){
		Double a = Double.parseDouble(StringUtils.isNotBlank(value1)?value1:"0");
		Double b = Double.parseDouble(StringUtils.isNotBlank(value2)?value2:"0");

		return String.format("%.3f",(a + b));
	}

	public static String multiply(String value1,String value2){
		Double a = Double.parseDouble(StringUtils.isNotBlank(value1)?value1:"0");
		Double b = Double.parseDouble(StringUtils.isNotBlank(value2)?value2:"0");

		return String.format("%.2f",(a * b));
	}

	public static byte[] hexStr2bytes(String hex) {
		hex=hex.trim().replaceAll(" ","");
		int len = (hex.length() / 2);
		byte[] result = new byte[len];
		char[] achar = hex.toUpperCase().toCharArray();
		for (int i = 0; i < len; i++) {
			int pos = i * 2;
			result[i] = (byte) (hexChar2byte(achar[pos]) << 4 | hexChar2byte(achar[pos + 1]));
		}
		return result;
	}

	public static String bytes2HexStr(byte src){
		StringBuilder builder = new StringBuilder();
		char[] buffer = new char[2];
		buffer[0] = Character.forDigit((src >>> 4) & 0x0F, 16);
		buffer[1] = Character.forDigit(src & 0x0F, 16);
		builder.append(buffer);
		return builder.toString().toUpperCase();
	}

	public static String bytes2HexStr(byte[] src) {
		StringBuilder builder = new StringBuilder();
		if (src == null || src.length <= 0) {
			return "";
		}
		char[] buffer = new char[2];
		for (byte b : src) {
			buffer[0] = Character.forDigit((b >>> 4) & 0x0F, 16);
			buffer[1] = Character.forDigit(b & 0x0F, 16);
			builder.append(buffer);
		}
		return builder.toString().toUpperCase();
	}

	private static int hexChar2byte(char c) {
		switch (c) {
			case '0':
				return 0;
			case '1':
				return 1;
			case '2':
				return 2;
			case '3':
				return 3;
			case '4':
				return 4;
			case '5':
				return 5;
			case '6':
				return 6;
			case '7':
				return 7;
			case '8':
				return 8;
			case '9':
				return 9;
			case 'a':
			case 'A':
				return 10;
			case 'b':
			case 'B':
				return 11;
			case 'c':
			case 'C':
				return 12;
			case 'd':
			case 'D':
				return 13;
			case 'e':
			case 'E':
				return 14;
			case 'f':
			case 'F':
				return 15;
			default:
				return -1;
		}
	}

	public static void main(String[] args) {
		byte[] aaa = CalculateUtils.hexStr2bytes("6822100301167968110400000000");
 		byte checkSum = 0x00;
		for(byte a:aaa){
			checkSum  += (a & 0xFF);
		}
		System.out.println(CalculateUtils.bytes2HexStr(checkSum));
	}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值