CmdHandler



/**
 * Created by gcy on 2018/1/9 0009.
 */

public class CmdHandler {

    public static byte xor(byte... bytes) {
        byte result = (byte) (bytes[0] ^ bytes[1]);
        for (int i = 2; i < bytes.length; ++i) {
            result = (byte) (result ^ bytes[i]);
        }

        return result;
    }



    public static byte[] bytes2HexBytes(byte[] bytes) {
        byte[] result = new byte[bytes.length];
        for (int i = 0; i < bytes.length; i++) {

            result[i] = (byte) (bytes[i] & 0xFF);
        }
        return result;
    }




    public static String bytes2HexString(byte[] bytes) {
        String ret = "";
        for (int i = 0; i < bytes.length; i++) {
            String hex = Integer.toHexString(bytes[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            ret += (hex.toUpperCase());
        }
        return ret;
    }


    // 位数由高到低依次作为参数传入
    public static int byte2Int(byte... bytes) {
        int length = bytes.length;
        int result = 0;
        for (int i = length; i > 0; i--) {
            int temp = ((bytes[i - 1] & 0xff) << (length - i) * 8);
            result = result | temp;
        }
        return result;
    }




    public static String printHexString(byte[] b) {
        String a = "";
        for (int i = 0; i < b.length; i++) {
            String hex = Integer.toHexString(b[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }

            a = a + hex;
        }

        return a;
    }

    public static double formatWeight(float v) {
        BigDecimal decimal = new BigDecimal(String.valueOf(v));
        DecimalFormat format = new DecimalFormat("####.#");
        format.setRoundingMode(RoundingMode.HALF_UP);
        return Double.parseDouble(format.format(decimal.doubleValue()));
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值