[android]通讯中常用到的Helper

本文介绍了ByteHelper、BCDHelper及CheckHelper三个工具类,分别用于实现不同数据类型之间的转换、BCD码的压缩与解析以及CRC校验与异或校验等功能。通过这些工具类,可以方便地进行如整数到字节数组、字符串到GBK编码等转换。

1.常用类型和byte[]互转的ByteHelper ;

2.BCD码压缩的BCDHelper ;

3.CRC、XOR的CheckHelper  。

 

ByteHelper

package  com.van.base;

import  java.io.UnsupportedEncodingException;

public  class  ByteHelper {
 
    /**
    * @Title: UInt16ToByte2
    * @Description: TODO(将UInt16转化为byte2)
    *
    * 
@param  Val
    * 
@return
    * 
@return :byte[]
    *
    * 
@version : v1.0.0
    * 
@author : WANJJ
    * @date: 2011-11-17
    * 
    * Modification History: 
    * Date  Author  Version  Description
    * ---------------------------------------------------------*
    * 2011-11-17  wanjj v1.0.0   修改原因
    
*/
    
    public  static  byte [] UInt16ToByte2(Integer Val) {
        byte [] bts = new  byte [2];
        bts[0] = (byte ) ((0xff00 & Val) >> 8);
        bts[1] = (byte ) (0xff & Val);
        return  bts;
    }
 
    /**
    * @Title: UInt16ToByte2
    * @Description: TODO(将UInt16转化为byte2)
    *
    * 
@param  i
    * 
@param  bts
    * 
@param  offset
    * 
@return :void
    *
    * 
@version : v1.0.0
    * 
@author : WANJJ
    * @date: 2011-11-17
    * 
    * Modification History: 
    * Date  Author  Version  Description
    * ---------------------------------------------------------*
    * 2011-11-17  wanjj v1.0.0   修改原因
    
*/
    
    public  static  void  UInt16ToByte2(Integer i, byte [] bts, int  offset) {
        byte [] btsTemp = UInt16ToByte2(i);
        int  index = 0;
        while  (index < 2) {
            bts[index + offset] = btsTemp[index];
            index += 1;
        }
    }
 
    /**
    * @Title: Int16ToByte2
    * @Description: TODO(将Int16转化为byte2)
    *
    * 
@param  Val
    * 
@return
    * 
@return :byte[]
    *
    * 
@version : v1.0.0
    * 
@author : WANJJ
    * @date: 2011-11-17
    * 
    * Modification History: 
    * Date  Author  Version  Description
    * ---------------------------------------------------------*
    * 2011-11-17  wanjj v1.0.0   修改原因
    
*/
    
    public  static  byte [] Int16ToByte2(Short Val) {
        byte [] bts = new  byte [2];
        bts[0] = (byte ) ((Val >> 8) & 0xff);
        bts[1] = (byte ) (Val & 0xff);
        return  bts;
    }
 
    /**
    * @Title: Int16ToByte2
    * @Description: TODO(将Int16转化为byte2)
    *
    * 
@param  i
    * 
@param  bts
    * 
@param  offset
    * 
@return :void
    *
    * 
@version : v1.0.0
    * 
@author : WANJJ
    * @date: 2011-11-17
    * 
    * Modification History: 
    * Date  Author  Version  Description
    * ---------------------------------------------------------*
    * 2011-11-17  wanjj v1.0.0   修改原因
    
*/
    
    public  static  void  Int16ToByte2(Short i, byte [] bts, int  offset) {
        byte [] btsTemp = Int16ToByte2(i);
        int  index = 0;
        while  (index < 2) {
            bts[index + offset] = btsTemp[index];
            index += 1;
        }
    }
 
    /**
    * @Title: UIntToByte4
    * @Description: TODO(将Uint32转化为byte4)
    *
    * 
@param  Val
    * 
@return
    * 
@return :byte[]
    *
    * 
@version : v1.0.0
    * 
@author : WANJJ
    * @date: 2011-11-17
    * 
    * Modification History: 
    * Date  Author  Version  Description
    * ---------------------------------------------------------*
    * 2011-11-17  wanjj v1.0.0   修改原因
    
*/
    
    public  static  byte [] UIntToByte4(Long Val) {
        byte [] bts = new  byte [4];
        bts[0] = (byte ) ((0xff000000L & Val) >> 24);
        bts[1] = (byte ) ((0xff0000 & Val) >> 16);
        bts[2] = (byte ) ((0xff00 & Val) >> 8);
        bts[3] = (byte ) (0xff & Val);
        return  bts;
    }
 
    /**
    * @Title: UIntToByte4
    * @Description: TODO(将Uint32转化为byte4)
    *
    * 
@param  i
    * 
@param  bts
    * 
@param  offset
    * 
@return :void
    *
    * 
@version : v1.0.0
    * 
@author : WANJJ
    * @date: 2011-11-17
    * 
    * Modification History: 
    * Date  Author  Version  Description
    * ---------------------------------------------------------*
    * 2011-11-17  wanjj v1.0.0   修改原因
    
*/
    
    public  static  void  UIntToByte4(Long i, byte [] bts, int  offset) {
        byte [] btsTemp = UIntToByte4(i);
        int  index = 0;
        while  (index < 4) {
            bts[index + offset] = btsTemp[index];
            index += 1;
        }
    }
 
    /**
    * @Title: IntToByte4
    * @Description: TODO(将int32转化为byte4)
    *
    * 
@param  Val
    * 
@return
    * 
@return :byte[]
    *
    * 
@version : v1.0.0
    * 
@author : WANJJ
    * @date: 2011-11-17
    * 
    * Modification History: 
    * Date  Author  Version  Description
    * ---------------------------------------------------------*
    * 2011-11-17  wanjj v1.0.0   修改原因
    
*/
    
    public  static  byte [] IntToByte4(Integer Val) {
        byte [] bts = new  byte [4];
        bts[0] = (byte ) ((0xff000000 & Val) >> 24);
        bts[1] = (byte ) ((0xff0000 & Val) >> 16);
        bts[2] = (byte ) ((0xff00 & Val) >> 8);
        bts[3] = (byte ) (0xff & Val);
        return  bts;
    } 
    /**
    * @Title: IntToByte4
    * @Description: TODO(将int32转化为byte4)
    *
    * 
@param  i
    * 
@param  bts
    * 
@param  offset
    * 
@return :void
    *
    * 
@version : v1.0.0
    * 
@author : WANJJ
    * @date: 2011-11-17
    * 
    * Modification History: 
    * Date  Author  Version  Description
    * ---------------------------------------------------------*
    * 2011-11-17  wanjj v1.0.0   修改原因
    
*/
    
    public  static  void  IntToByte4(Integer i, byte [] bts, int  offset) {
        byte [] btsTemp = IntToByte4(i);
        int  index = 0;
        while  (index < 4) {
            bts[index + offset] = btsTemp[index];
            index += 1;
        }
    }
 
    /**
    * @Title: FloatToByte4
    * @Description: TODO(Float转byte4)
    *
    * 
@param  val
    * 
@return
    * 
@return :byte[]
    *
    * 
@version : v1.0.0
    * 
@author : WANJJ
    * @date: 2011-11-17
    * 
    * Modification History: 
    * Date  Author  Version  Description
    * ---------------------------------------------------------*
    * 2011-11-17  wanjj v1.0.0   修改原因
    
*/
    
    public  static  byte [] FloatToByte4(float  val) {
        return  IntToByte4(Float.floatToIntBits(val));
    } 
    /**
    * @Title: FloatToByte4
    * @Description: TODO(Float转byte4)
    *
    * 
@param  val
    * 
@param  bts
    * 
@param  offset
    * 
@return :void
    *
    * 
@version : v1.0.0
    * 
@author : WANJJ
    * @date: 2011-11-17
    * 
    * Modification History: 
    * Date  Author  Version  Description
    * ---------------------------------------------------------*
    * 2011-11-17  wanjj v1.0.0   修改原因
    
*/
    
    public  static  void  FloatToByte4(float  val, byte [] bts, int  offset) {
        byte [] btsTemp = FloatToByte4(val);
        int  index = 0;
        while  (index < 4) {
            bts[index + offset] = btsTemp[index];
            index += 1;
        }
    }

    /**
     * @Title: Byte2ToInt16
     * @Description: TODO(Byte2转int16)
     * 
     * 
@param  bt1
     * 
@param  bt2
     * 
@return
     * 
@return :short
     * 
     * 
@version : v1.0.0
     * 
@author : WANJJ
     * @date: 2011-11-8
     * 
     *        Modification History: Date Author Version Description
     *        ---------------------------------------------------------*
     *        2011-11-8 wanjj v1.0.0 修改原因
     
*/

    public  static  short  Byte2ToInt16(byte  bt1, byte  bt2) {
        return  (short ) ((bt1 & 0xff) << 8 | bt2 & 0xff);

    }

    /**
     * @Title: Byte2ToInt16
     * @Description: TODO(Byte2转int16)
     * 
     * 
@param  bts
     * 
@param  offset
     * 
@return
     * 
@return :short
     * 
     * 
@version : v1.0.0
     * 
@author : WANJJ
     * @date: 2011-11-8
     * 
     *        Modification History: Date Author Version Description
     *        ---------------------------------------------------------*
     *        2011-11-8 wanjj v1.0.0 修改原因
     
*/

    public  static  short  Byte2ToInt16(byte [] bts, int  offset) {
        return  Byte2ToInt16(bts[offset], bts[offset + 1]);

    }

    /**
     * @Title: Byte2ToUInt16
     * @Description: TODO(Byte2转uint16)
     * 
     * 
@param  bt1
     * 
@param  bt2
     * 
@return
     * 
@return :Integer
     * 
     * 
@version : v1.0.0
     * 
@author : WANJJ
     * @date: 2011-11-8
     * 
     *        Modification History: Date Author Version Description
     *        ---------------------------------------------------------*
     *        2011-11-8 wanjj v1.0.0 修改原因
     
*/

    public  static  Integer Byte2ToUInt16(byte  bt1, byte  bt2) {
        return  (int ) ((bt1 & 0xff) << 8 | bt2 & 0xff);
    }

    /**
     * @Title: Byte2ToUInt16
     * @Description: TODO( Byte2转uint16)
     * 
     * 
@param  bts
     * 
@param  offset
     * 
@return
     * 
@return :Integer
     * 
     * 
@version : v1.0.0
     * 
@author : WANJJ
     * @date: 2011-11-8
     * 
     *        Modification History: Date Author Version Description
     *        ---------------------------------------------------------*
     *        2011-11-8 wanjj v1.0.0 修改原因
     
*/

    public  static  Integer Byte2ToUInt16(byte [] bts, int  offset) {
        return  Byte2ToUInt16(bts[offset], bts[offset + 1]);
    }

    /**
     * @Title: Byte4ToInt32
     * @Description: TODO(Byte4转int32)
     * 
     * 
@param  bt1
     * 
@param  bt2
     * 
@param  bt3
     * 
@param  bt4
     * 
@return
     * 
@return :Integer
     * 
     * 
@version : v1.0.0
     * 
@author : WANJJ
     * @date: 2011-11-8
     * 
     *        Modification History: Date Author Version Description
     *        ---------------------------------------------------------*
     *        2011-11-8 wanjj v1.0.0 修改原因
     
*/

    public  static  Integer Byte4ToInt32(byte  bt1, byte  bt2, byte  bt3, byte  bt4) {
        return  (bt1 & 0xff) << 24 | (bt2 & 0xff) << 16 | (bt3 & 0xff) << 8
                | bt4 & 0xff;

    }

    /**
     * @Title: Byte4ToInt32
     * @Description: TODO(Byte4转int32)
     * 
     * 
@param  bts
     * 
@param  offset
     * 
@return
     * 
@return :Integer
     * 
     * 
@version : v1.0.0
     * 
@author : WANJJ
     * @date: 2011-11-8
     * 
     *        Modification History: Date Author Version Description
     *        ---------------------------------------------------------*
     *        2011-11-8 wanjj v1.0.0 修改原因
     
*/

    public  static  Integer Byte4ToInt32(byte [] bts, int  offset) {
        return  Byte4ToInt32(bts[offset], bts[offset + 1], bts[offset + 2],
                bts[offset + 3]);

    }

    /**
     * @Title: Byte4ToUInt32
     * @Description: TODO(Byte4转uint32)
     * 
     * 
@param  bt1
     * 
@param  bt2
     * 
@param  bt3
     * 
@param  bt4
     * 
@return
     * 
@return :long
     * 
     * 
@version : v1.0.0
     * 
@author : WANJJ
     * @date: 2011-11-8
     * 
     *        Modification History: Date Author Version Description
     *        ---------------------------------------------------------*
     *        2011-11-8 wanjj v1.0.0 修改原因
     
*/

    public  static  long  Byte4ToUInt32(byte  bt1, byte  bt2, byte  bt3, byte  bt4) {

        return  (long ) Byte4ToInt32(bt1, bt2, bt3, bt4);
    }

    /**
     * @Title: Byte4ToUInt32
     * @Description: TODO(Byte4转uint32)
     * 
     * 
@param  bts
     * 
@param  offset
     * 
@return
     * 
@return :long
     * 
     * 
@version : v1.0.0
     * 
@author : WANJJ
     * @date: 2011-11-8
     * 
     *        Modification History: Date Author Version Description
     *        ---------------------------------------------------------*
     *        2011-11-8 wanjj v1.0.0 修改原因
     
*/

    public  static  long  Byte4ToUInt32(byte [] bts, int  offset) {
        return  Byte4ToUInt32(bts[offset], bts[offset + 1], bts[offset + 2],
                bts[offset + 3]);
    }

    /**
     * @Title: Byte4ToFloat
     * @Description: TODO(Byte4转Float)
     * 
     * 
@param  bt1
     * 
@param  bt2
     * 
@param  bt3
     * 
@param  bt4
     * 
@return
     * 
@return :float
     * 
     * 
@version : v1.0.0
     * 
@author : WANJJ
     * @date: 2011-11-8
     * 
     *        Modification History: Date Author Version Description
     *        ---------------------------------------------------------*
     *        2011-11-8 wanjj v1.0.0 修改原因
     
*/

    public  static  float  Byte4ToFloat(byte  bt1, byte  bt2, byte  bt3, byte  bt4) {
        return  Float.intBitsToFloat(Byte4ToInt32(bt1, bt2, bt3, bt4));
    }

    /**
     * @Title: Byte4ToFloat
     * @Description: TODO(Byte4转Float)
     * 
     * 
@param  bts
     * 
@param  offset
     * 
@return
     * 
@return :float
     * 
     * 
@version : v1.0.0
     * 
@author : WANJJ
     * @date: 2011-11-8
     * 
     *        Modification History: Date Author Version Description
     *        ---------------------------------------------------------*
     *        2011-11-8 wanjj v1.0.0 修改原因
     
*/

    public  static  float  Byte4ToFloat(byte [] bts, int  offset) {
        return  Byte4ToFloat(bts[offset], bts[offset + 1], bts[offset + 2],
                bts[offset + 3]);
    }

    /**
     * @Title: StringToGBK
     * @Description: TODO(字符串转GBK编码)
     * 
     * 
@param  str
     * 
@return
     * 
@throws  UnsupportedEncodingException
     * 
@return :byte[]
     * 
     * 
@version : v1.0.0
     * 
@author : WANJJ
     * @date: 2011-11-8
     * 
     *        Modification History: Date Author Version Description
     *        ---------------------------------------------------------*
     *        2011-11-8 wanjj v1.0.0 修改原因
     
*/

    public  static  byte [] StringToGBK(String str)
            throws  UnsupportedEncodingException {

        return  str.getBytes("GBK");
    }

    /**
     * @Title: FillSGBK
     * @Description: TODO(填充GBK编码)
     * 
     * 
@param  str
     * 
@param  bts
     * 
@param  offset
     * 
@return
     * 
@throws  UnsupportedEncodingException
     * 
@return :int
     * 
     * 
@version : v1.0.0
     * 
@author : WANJJ
     * @date: 2011-11-8
     * 
     *        Modification History: Date Author Version Description
     *        ---------------------------------------------------------*
     *        2011-11-8 wanjj v1.0.0 修改原因
     
*/

    public  static  int  FillSGBK(String str, byte [] bts, int  offset)
            throws  UnsupportedEncodingException {
        return  FillSGBK(str, bts, offset, 0);
    }

    /**
     * @Title: FillSGBK
     * @Description: TODO(填充GBK编码)
     * 
     * 
@param  str
     * 
@param  bts
     * 
@param  offset
     * 
@param  length
     * 
@return
     * 
@throws  UnsupportedEncodingException
     * 
@return :int
     * 
     * 
@version : v1.0.0
     * 
@author : WANJJ
     * @date: 2011-11-8
     * 
     *        Modification History: Date Author Version Description
     *        ---------------------------------------------------------*
     *        2011-11-8 wanjj v1.0.0 修改原因
     
*/

    public  static  int  FillSGBK(String str, byte [] bts, int  offset, int  length)
            throws  UnsupportedEncodingException {
        int  len = 0;
        if  (str != null ) {
            byte [] tmp = StringToGBK(str);
            //  填充数据
            for  (int  i = 0; i < tmp.length; i++) {
                bts[offset + i] = tmp[i];
            }
            len = tmp.length;
        }
        //  补0
        for  (int  i = len; i < length; i++) {
            bts[offset + i] = 0;
        }
        return  len > length ? len : length;
    }

    /**
     * @Title: GBKToString
     * @Description: TODO(GBK编码转字符串)
     * 
     * 
@param  bts
     * 
@return
     * 
@throws  UnsupportedEncodingException
     * 
@return :String
     * 
     * 
@version : v1.0.0
     * 
@author : WANJJ
     * @date: 2011-11-8
     * 
     *        Modification History: Date Author Version Description
     *        ---------------------------------------------------------*
     *        2011-11-8 wanjj v1.0.0 修改原因
     
*/

    public  static  String GBKToString(byte [] bts)
            throws  UnsupportedEncodingException {
        return  GBKToString(bts, 0);
    }

    /**
     * @Title: GBKToString
     * @Description: TODO(GBK编码转字符串)
     * 
     * 
@param  bts
     * 
@param  offset
     * 
@return
     * 
@throws  UnsupportedEncodingException
     * 
@return :String
     * 
     * 
@version : v1.0.0
     * 
@author : WANJJ
     * @date: 2011-11-8
     * 
     *        Modification History: Date Author Version Description
     *        ---------------------------------------------------------*
     *        2011-11-8 wanjj v1.0.0 修改原因
     
*/

    public  static  String GBKToString(byte [] bts, int  offset)
            throws  UnsupportedEncodingException {
        return  SGBKToString(bts, offset, bts.length - offset);
    }

    /**
     * @Title: SGBKToString
     * @Description: TODO(GBK编码转字符串(定长字符串))
     * 
     * 
@param  bts
     * 
@param  offset
     * 
@param  count
     * 
@return
     * 
@throws  UnsupportedEncodingException
     * 
@return :String
     * 
     * 
@version : v1.0.0
     * 
@author : WANJJ
     * @date: 2011-11-8
     * 
     *        Modification History: Date Author Version Description
     *        ---------------------------------------------------------*
     *        2011-11-8 wanjj v1.0.0 修改原因
     
*/

    public  static  String SGBKToString(byte [] bts, int  offset, int  count)
            throws  UnsupportedEncodingException {
        return  new  String(bts, offset, count, "GBK");
    }

    /**
     * @Title: HexStringToBytes
     * @Description: TODO(HEX字符串转BYTE数组)
     * 
     * 
@param  hex
     * 
@return
     * 
@return :byte[]
     * 
     * 
@version : v1.0.0
     * 
@author : WANJJ
     * @date: 2011-11-8
     * 
     *        Modification History: Date Author Version Description
     *        ---------------------------------------------------------*
     *        2011-11-8 wanjj v1.0.0 修改原因
     
*/

    public  static  byte [] HexStringToBytes(String hex) {
        byte [] bts = new  byte [hex.length() / 2];
        for  (int  i = 0; i < bts.length; i++) {
            bts[i] = (byte ) Integer.parseInt(hex.substring(2 * i, 2 * i + 2),
                    16);
        }
        return  bts;
    }

    /**
     * @Title: BytesToHexString
     * @Description: TODO(BYTE数组转HEX字符串)
     * 
     * 
@param  bts
     * 
@return
     * 
@return :String
     * 
     * 
@version : v1.0.0
     * 
@author : WANJJ
     * @date: 2011-11-8
     * 
     *        Modification History: Date Author Version Description
     *        ---------------------------------------------------------*
     *        2011-11-8 wanjj v1.0.0 修改原因
     
*/

    public  static  String BytesToHexString(byte [] bts) {
        return  BytesToHexString(bts, 0, bts.length);
    }

    /**
     * @Title: BytesToHexString
     * @Description: TODO(BYTE数组转HEX字符串)
     * 
     * 
@param  bts
     * 
@param  offset
     * 
@param  count
     * 
@return
     * 
@return :String
     * 
     * 
@version : v1.0.0
     * 
@author : WANJJ
     * @date: 2011-11-8
     * 
     *        Modification History: Date Author Version Description
     *        ---------------------------------------------------------*
     *        2011-11-8 wanjj v1.0.0 修改原因
     
*/

    public  static  String BytesToHexString(byte [] bts, int  offset, int  count) {
        StringBuilder sb = new  StringBuilder(bts.length * 2);
        for  (int  i = 0; i < count; i++) {
            sb.append(Integer.toHexString(bts[i + offset]));
        }
        return  sb.toString();
    }

    public  static  int  BoolToByte(boolean  bl) {

        return  bl ? 1 : 0 << 0;
    }
}

 

 


 

 BCDHelper

package  com.van.base;

import  java.io.UnsupportedEncodingException;

import  android.text.format.Time;

public  class  BCDHelper { 
    /**
    * @Title: StrToBCD
    * @Description: TODO(用BCD码压缩数字字符串)
    *
    * 
@param  str
    * 
@return
    * 
@return :byte[]
    *
    * 
@version : v1.0.0
    * 
@author : WANJJ
    * @date: 2011-11-17
    * 
    * Modification History: 
    * Date  Author  Version  Description
    * ---------------------------------------------------------*
    * 2011-11-17  wanjj v1.0.0   修改原因
    
*/
    
    public  static  byte [] StrToBCD(String str) {

        return  StrToBCD(str, str.length());
    }

    public  static  byte [] StrToBCD(String str, int  numlen) {
        if  (numlen % 2 != 0)
            numlen++;

        while  (str.length() < numlen) {
            str = "0" + str;
        }

        byte [] bStr = new  byte [str.length() / 2];
        char [] cs = str.toCharArray();
        int  i = 0;
        int  iNum = 0;
        for  (i = 0; i < cs.length; i += 2) {

            int  iTemp = 0;
            if  (cs[i] >= '0' && cs[i] <= '9') {
                iTemp = (cs[i] - '0') << 4;
            } else  {
                //  判断是否为a~f
                if  (cs[i] >= 'a' && cs[i] <= 'f') {
                    cs[i] -= 32;
                }
                iTemp = (cs[i] - '0' - 7) << 4;
            }
            //  处理低位
            if  (cs[i + 1] >= '0' && cs[i + 1] <= '9') {
                iTemp += cs[i + 1] - '0';
            } else  {
                //  判断是否为a~f
                if  (cs[i + 1] >= 'a' && cs[i + 1] <= 'f') {
                    cs[i + 1] -= 32;
                }
                iTemp += cs[i + 1] - '0' - 7;
            }
            bStr[iNum] = (byte ) iTemp;
            iNum++;
        }
        return  bStr;

    }

    /**
    * @Title: bcdToInt
    * @Description: TODO(BCD转int)
    *
    * 
@param  bcdNum
    * 
@param  offset
    * 
@param  numlen
    * 
@return
    * 
@return :int
    *
    * 
@version : v1.0.0
    * 
@author : WANJJ
    * @date: 2011-11-17
    * 
    * Modification History: 
    * Date  Author  Version  Description
    * ---------------------------------------------------------*
    * 2011-11-17  wanjj v1.0.0   修改原因
    
*/
    
    public  static  int  bcdToInt(byte [] bcdNum, int  offset, int  numlen) {
        return  Integer.parseInt(bcdToString(bcdNum, offset, numlen));
    }
 
    /**
    * @Title: bcdToString
    * @Description: TODO(BCD转字符串)
    *
    * 
@param  bcdNum
    * 
@param  offset
    * 
@param  numlen
    * 
@return
    * 
@return :String
    *
    * 
@version : v1.0.0
    * 
@author : WANJJ
    * @date: 2011-11-17
    * 
    * Modification History: 
    * Date  Author  Version  Description
    * ---------------------------------------------------------*
    * 2011-11-17  wanjj v1.0.0   修改原因
    
*/
    
    public  static  String bcdToString(byte [] bcdNum, int  offset, int  numlen) {
        int  len = numlen / 2;
        if  (numlen % 2 != 0)
            len++;

        StringBuffer sb = new  StringBuffer();
        for  (int  i = 0; i < len; i++) {
            sb.append(Integer.toHexString((bcdNum[i + offset] & 0xf0) >> 4));
            sb.append(Integer.toHexString(bcdNum[i + offset] & 0xf));
        }
        return  sb.toString();
    }
 
    /**
    * @Title: Bcd3ToDateTime
    * @Description: TODO(BCD码转日期)
    *
    * 
@param  data
    * 
@param  offset
    * 
@return
    * 
@return :Time
    *
    * 
@version : v1.0.0
    * 
@author : WANJJ
    * @date: 2011-11-17
    * 
    * Modification History: 
    * Date  Author  Version  Description
    * ---------------------------------------------------------*
    * 2011-11-17  wanjj v1.0.0   修改原因
    
*/
    
    public  static  Time Bcd3ToDateTime(byte [] data, int  offset) {
        int  year = 0, month = 0, monthDay = 0;
        year = Integer.parseInt("20" + bcdToString(data, offset, 2));
        month = bcdToInt(data, offset + 1, 2);
        monthDay = bcdToInt(data, offset + 2, 2);
        Time time = new  Time("GMT+8");
        time.set(monthDay, month, year);
        return  time;
    }

    public  static  Time Bcd5ToDateTime(byte [] data, int  offset) {
        int  year = 0, month = 0, monthDay = 0, hour = 0, minute = 0, second = 0;

        year = Integer.parseInt("20" + bcdToString(data, offset, 2));
        month = bcdToInt(data, offset + 1, 2);
        monthDay = bcdToInt(data, offset + 2, 2);
        hour = bcdToInt(data, offset + 3, 2);
        minute = bcdToInt(data, offset + 4, 2);

        Time time = new  Time("GMT+8");
        time.set(second, minute, hour, monthDay, month, year);
        return  time;
    } 
    /**
    * @Title: Bcd6ToDateTime
    * @Description: TODO(BCD码转时间格式)
    *
    * 
@param  data
    * 
@param  offset
    * 
@return
    * 
@return :Time
    *
    * 
@version : v1.0.0
    * 
@author : WANJJ
    * @date: 2011-11-17
    * 
    * Modification History: 
    * Date  Author  Version  Description
    * ---------------------------------------------------------*
    * 2011-11-17  wanjj v1.0.0   修改原因
    
*/
    
    public  static  Time Bcd6ToDateTime(byte [] data, int  offset) {
        int  year = 0, month = 0, monthDay = 0, hour = 0, minute = 0, second = 0;

        year = Integer.parseInt("20" + bcdToString(data, offset, 2));
        month = bcdToInt(data, offset + 1, 2);
        monthDay = bcdToInt(data, offset + 2, 2);
        hour = bcdToInt(data, offset + 3, 2);
        minute = bcdToInt(data, offset + 4, 2);
        second = bcdToInt(data, offset + 5, 2);

        Time time = new  Time("GMT+8");
        time.set(second, minute, hour, monthDay, month, year);
        return  time;
    }

    public  static  Time Bcd7ToDateTime(byte [] data, int  offset) {
        int  year = 0, month = 0, monthDay = 0, hour = 0, minute = 0, second = 0;

        year = bcdToInt(data, offset, 4);
        month = bcdToInt(data, offset + 2, 2);
        monthDay = bcdToInt(data, offset + 3, 2);
        hour = bcdToInt(data, offset + 4, 2);
        minute = bcdToInt(data, offset + 5, 2);
        second = bcdToInt(data, offset + 6, 2);

        Time time = new  Time("GMT+8");
        time.set(second, minute, hour, monthDay, month, year);
        return  time;
    }
 
    /**
    * @Title: DateTimeToBcd3
    * @Description: TODO(日期转BCD码)
    *
    * 
@param  dt
    * 
@return
    * 
@throws  UnsupportedEncodingException
    * 
@return :byte[]
    *
    * 
@version : v1.0.0
    * 
@author : WANJJ
    * @date: 2011-11-17
    * 
    * Modification History: 
    * Date  Author  Version  Description
    * ---------------------------------------------------------*
    * 2011-11-17  wanjj v1.0.0   修改原因
    
*/
    
    public  static  byte [] DateTimeToBcd3(Time dt)
            throws  UnsupportedEncodingException {

        StringBuilder sb = new  StringBuilder();
        sb.append(FStrLen(String.valueOf((dt.year - 2000)), 2));
        sb.append(FStrLen(String.valueOf((dt.month)), 2));
        sb.append(FStrLen(String.valueOf((dt.monthDay)), 2));

        return  StrToBCD(sb.toString());
    }

    public  static  byte [] DateTimeToBcd5(Time dt)
            throws  UnsupportedEncodingException {

        StringBuilder sb = new  StringBuilder();

        sb.append(FStrLen(String.valueOf((dt.year - 2000)), 2));
        sb.append(FStrLen(String.valueOf(dt.month), 2));
        sb.append(FStrLen(String.valueOf(dt.monthDay), 2));
        sb.append(FStrLen(String.valueOf(dt.hour), 2));
        sb.append(FStrLen(String.valueOf(dt.minute), 2));

        return  StrToBCD(sb.toString());
    }

    public  static  byte [] DateTimeToBcd6(Time dt)
            throws  UnsupportedEncodingException {
        StringBuilder sb = new  StringBuilder();

        sb.append(FStrLen(String.valueOf((dt.year - 2000)), 2));
        sb.append(FStrLen(String.valueOf(dt.month), 2));
        sb.append(FStrLen(String.valueOf(dt.monthDay), 2));
        sb.append(FStrLen(String.valueOf(dt.hour), 2));
        sb.append(FStrLen(String.valueOf(dt.minute), 2));
        sb.append(FStrLen(String.valueOf(dt.second), 2));

        return  StrToBCD(sb.toString());
    }

    public  static  byte [] DateTimeToBcd7(Time dt)
            throws  UnsupportedEncodingException {
        StringBuilder sb = new  StringBuilder();
        sb.append(FStrLen(String.valueOf(dt.year), 4));
        sb.append(FStrLen(String.valueOf(dt.month), 2));
        sb.append(FStrLen(String.valueOf(dt.monthDay), 2));
        sb.append(FStrLen(String.valueOf(dt.hour), 2));
        sb.append(FStrLen(String.valueOf(dt.minute), 2));
        sb.append(FStrLen(String.valueOf(dt.second), 2));
        return  StrToBCD(sb.toString());
    }

    public  static  String FStrLen(String str, int  len) {
        if  (str.length() < len) {
            str = "0" + str;
        }
        return  str;
    }

    /**
     * @Title: GetNowTime
     * @Description: TODO(获取当前GMT+8时间)
     * 
     * 
@return
     * 
@return :Time
     * 
     * 
@version : v1.0.0
     * 
@author : WANJJ
     * @date: 2011-11-9
     * 
     *        Modification History: Date Author Version Description
     *        ---------------------------------------------------------*
     *        2011-11-9 wanjj v1.0.0 修改原因
     
*/
    public  static  Time GetNowTime() {

        Time t = GetNewTime();
        t.setToNow();

        return  t;
    }

    public  static  Time GetNewTime() {
        return  new  Time("GMT+8");
    }

    /**
     * @Title: TimeDiff
     * @Description: TODO(计算两个时间相差毫秒数)
     * 
     * 
@param  tm1
     * 
@param  tm2
     * 
@return
     * 
@return :long
     * 
     * 
@version : v1.0.0
     * 
@author : WANJJ
     * @date: 2011-11-11
     * 
     *        Modification History: Date Author Version Description
     *        ---------------------------------------------------------*
     *        2011-11-11 wanjj v1.0.0 修改原因
     
*/

    public  static  long  TimeDiff(Time tm1, Time tm2) {
        return  (tm1.toMillis(true ) - tm2.toMillis(true ));
    }
}

 

 

 

 


 

CheckHelper

 

 

package  com.van.base;

import  java.util.ArrayList;

/**
 * 用户可以直接调用类里面的两个静态方法
 * 
 * 
@author  WANJJ
 * 
 
*/
public  class  CheckHelper {

    /**
     * 得出CRC计算结果
     * 
     * 
@param  buf
     *            要计算CRC的字符串
     * 
@return  整数
     
*/
    public  static  int  getCRC(String buf) {
        int  crc = 0xFFFF; //  initial value
        int  polynomial = 0x1021; //  0001 0000 0010 0001 (0, 5, 12)

        for  (int  j = 0; j < buf.length(); j++) {
            char  b = buf.charAt(j);
            for  (int  i = 0; i < 8; i++) {
                boolean  bit = ((b >> (7 - i) & 1) == 1);
                boolean  c15 = ((crc >> 15 & 1) == 1);
                crc <<= 1;
                if  (c15 ^ bit)
                    crc ^= polynomial;
            }
        }

        crc &= 0xffff;
        return  crc;
    }

    /**
     * 得出CRC计算结果
     * 
     * 
@param  buf
     *            要计算CRC的字符串
     * 
@return  字符串,2个字节
     
*/
    public  static  String getCRCString(String buf) {
        int  crc = 0xFFFF; //  initial value
        int  polynomial = 0x1021; //  0001 0000 0010 0001 (0, 5, 12)

        for  (int  j = 0; j < buf.length(); j++) {
            char  b = buf.charAt(j);
            for  (int  i = 0; i < 8; i++) {
                boolean  bit = ((b >> (7 - i) & 1) == 1);
                boolean  c15 = ((crc >> 15 & 1) == 1);
                crc <<= 1;
                if  (c15 ^ bit)
                    crc ^= polynomial;
            }
        }

        crc &= 0xffff;
        String str = "" + (char ) (crc / 256) + (char ) (crc % 256);
        return  str;
    }

    /**
     * 得出异或计算结果
     * 
     * 
@param  data
     *            要计算异或的数据
     * 
@param  offset
     *            偏移位
     * 
@param  length
     *            长度
     * 
@return  byte
     
*/
    public  static  byte  CheckXOR(byte [] data, int  offset, int  length) {
        byte  xda = 0;
        for  (int  i = 0; i < length; i++) {
            xda = (byte ) ((data[i + offset] ^ xda) & 0xFF);
        }
        return  xda;
    }

    /**
     * 校验并转义(异或效验)
     * 
     * 
@param  data
     *            要计算异或的数据
     * 
@param  offset
     *            偏移位
     * 
@param  length
     *            长度
     * 
@return  byte
     
*/
    public  static  void  XORAndEscape(ArrayList<Byte> data, int  offset, int  length) {
        byte  xda = 0;
        for  (int  i = offset; i < offset + length; i++) {
            xda = (byte ) (data.get(i) ^ xda);
            if  (data.get(i) == 0x7e)//  转义 向后插入一个0x02
            {
                data.set(i, (byte ) 0x7d);
                data.add(i + 1, (byte ) 0x02);
                length++;
                i++;
            } else  if  (data.get(i) == 0x7d)//  转义 向后插入一个0x01
            {
                data.add(i + 1, (byte ) 0x01);
                length++;
                i++;
            }
        }
        data.add(offset + length, xda);
        if  (xda == 0x7e)//  转义 向后插入一个0x02
            data.add(offset + length + 1, (byte ) 0x02);
        else  if  (xda == 0x7d)//  转义 向后插入一个0x01
            data.add(offset + length + 1, (byte ) 0x01);
    }
}

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值