TypeChangeUtil

public class TypeChangeUtil {
    /**
     * 把int转成DWORD
     */
    public static byte[] IntToDWORD(int val){
        byte[] result = new byte[4];
        String binary = Integer.toBinaryString(val);
        StringBuffer buffer = new StringBuffer(binary);
        for(int i=binary.length();i<32;i++){
            buffer.insert(0, "0");
        }
        for(int i=0;i<4;i++){
            String vv  = buffer.substring(8*i, 8*(i+1));
            BigInteger vals = new BigInteger(vv,2);
            result[i] = vals.byteValue();
        }
        return result;
    }
   
    /**
     * 把int转成WORD
     * @param args
     */
    public static byte[] IntToWORD(int val){
        byte[] result = new byte[2];
        String binary = Integer.toBinaryString(val);
        StringBuffer buffer = new StringBuffer(binary);
        for(int i=binary.length();i<16;i++){
            buffer.insert(0, "0");
        }
        for(int i=0;i<2;i++){
            String vv  = buffer.substring(8*i, 8*(i+1));
            BigInteger vals = new BigInteger(vv,2);
            result[i] = vals.byteValue();
        }
        return result;
    }
    /**
     * 把WORD转成int
     * @param args
     */
    public static int WORDToInt(byte[] val){
        StringBuffer rst = new StringBuffer();
        for(int i=0;i<2;i++){
            int n = ByteToInt(val[i]);
            StringBuffer bin = new StringBuffer(Integer.toBinaryString(n));
            for(int j=bin.toString().length();j<8;j++){
                bin.insert(0, "0");
            }
            rst.append(bin.toString());
        }
        BigInteger result = new BigInteger(rst.toString(),2);
        return result.intValue();
    }
    /**
     * 把16进制字符串转成int(WORD,DWORD通用)
     * @param hex
     * @return
     */
    public static int hexStringToInt(String hex){
        BigInteger bint = new BigInteger(hex,16);
        return bint.intValue();
    }
    /**
     * 把byte数组转成int
     * @param args
     */
    public static int ByteToInt(byte[] val){
        StringBuffer buffer = new StringBuffer();
        for(byte b:val){
            String bin = Integer.toBinaryString(b);
            StringBuffer standardBin = new StringBuffer(bin);
            for(int i=bin.length();i<8;i++){
                standardBin.insert(0, "0");
            }
            buffer.append(standardBin.toString());
        }
        BigInteger rs = new BigInteger(buffer.toString(),2);
        return rs.intValue();
    }
    /**
     * 把byte转成int
     * @param args
     */
    public static int ByteToInt(byte val){
        int result = val&0xff; 
        return result;
    }

    private static byte toByte(char c) {
        byte b = (byte) "0123456789ABCDEF".indexOf(c);
        return b;
    }
    /**
     * 什么是8421码,就是指把十进制数转成二进制,如
     * 91 转成二进制就是 1001 0001
     * 那它的8421码就是 10010001转成byte 就是
     */
    /**
     * 把数字转成8421BCD码
     * @param hex
     * @return
     */
    public static byte[] hexStringToByte(String hex) {
        if(hex.length()%2==1){
            hex = "0"+hex;
        }
        int len = (hex.length() / 2);
        byte[] result = new byte[len];
        char[] achar = hex.toCharArray();
        for (int i = 0; i < len; i++) {
         int pos = i * 2;
         result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
        }
        return result;
    }
   
    /**
     * 把8421BCD码转成数字
     * @param c
     * @return
     */
    public static Integer byteBCDToIntgeger(byte number){
        String binary = Integer.toBinaryString(ByteToInt(number));
        StringBuffer buffer = new StringBuffer(binary);
        for(int i=binary.length();i<8;i++){
            buffer.insert(0, "0");
        }
        BigInteger first = new BigInteger(buffer.substring(0, 4),2);
        BigInteger second = new BigInteger(buffer.substring(4, 8),2);
        String result= first+""+second;
        return Integer.valueOf(result);
    }
    /**
     * BCD号码解析
     * @return
     */
    public static StringBuffer analysisData(byte[] vinfo){
        StringBuffer checktel = new StringBuffer();
        StringBuffer buffer = new StringBuffer();
        StringBuffer t = new StringBuffer();
        int i=0;
        byte[] tel = null;
        while(i<(vinfo.length/6)){
            tel = new byte[6];
            System.arraycopy(vinfo, i*6, tel, 0, 6);
            for(int j=0;j<tel.length;j++){
                t.append(TypeChangeUtil.byteBCDToIntgeger(tel[j])+"");
                if(t.length()==1&&j!=0){
                    t.insert(0, "0");
                }
                buffer.append(t.toString());
                t.delete(0, t.length());
            }
            checktel.append(buffer.toString()+",");
            buffer.delete(0, buffer.length());
            i++;
        }
        return checktel;
    }
    /**
     * byte数组转16进制字符串
     * @param b
     * @return
     */
    public static String bytes2HexString(byte[] b) {
        String ret = "";
        for (int i = 0; i < b.length; i++) {
            String hex = Integer.toHexString(b[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            ret += hex.toUpperCase();
        }
        return ret;
    }
   
    public static void main(String[] args) {
        /*byte[] c= new byte[]{1,80,35,16,99,40};
        for(int i=0;i<c.length;i++){
            System.out.print(TypeChangeUtil.byteBCDToIntgeger(c[i]));
            //15994833240
            //13172486932
        }*/
    }
   
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值