之前在JT/T 808在android上实现时用到了不少类型转换的Helper,现将其公布出来,有需要的朋友拿去用吧。

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

2.BCD码压缩的BCDHelper;

3.CRC、XOR的CheckHelper。


ByteHelper

packagecom.van.base;

importjava.io.UnsupportedEncodingException;

public classByteHelper{

/**
*@Title:UInt16ToByte2
*@Description:TODO(将UInt16转化为byte2)
*
*
@param Val
*
@return
*
@return :byte[]
*
*
@version :v1.0.0
*
@author :WANJJ
*@date:2011-11-17
*
*ModificationHistory:
*DateAuthorVersionDescription
*---------------------------------------------------------*
*2011-11-17wanjjv1.0.0修改原因
*/

public static byte[]UInt16ToByte2(IntegerVal){
byte[]bts= new byte[2];
bts[0]=( byte)((0xff00&Val)>>8);
bts[1]=( byte)(0xff&Val);
returnbts;
}

/**
*@Title:UInt16ToByte2
*@Description:TODO(将UInt16转化为byte2)
*
*
@param i
*
@param bts
*
@param offset
*
@return :void
*
*
@version :v1.0.0
*
@author :WANJJ
*@date:2011-11-17
*
*ModificationHistory:
*DateAuthorVersionDescription
*---------------------------------------------------------*
*2011-11-17wanjjv1.0.0修改原因
*/

public static voidUInt16ToByte2(Integeri, byte[]bts, intoffset){
byte[]btsTemp=UInt16ToByte2(i);
intindex=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
*
*ModificationHistory:
*DateAuthorVersionDescription
*---------------------------------------------------------*
*2011-11-17wanjjv1.0.0修改原因
*/

public static byte[]Int16ToByte2(ShortVal){
byte[]bts= new byte[2];
bts[0]=( byte)((Val>>8)&0xff);
bts[1]=( byte)(Val&0xff);
returnbts;
}

/**
*@Title:Int16ToByte2
*@Description:TODO(将Int16转化为byte2)
*
*
@param i
*
@param bts
*
@param offset
*
@return :void
*
*
@version :v1.0.0
*
@author :WANJJ
*@date:2011-11-17
*
*ModificationHistory:
*DateAuthorVersionDescription
*---------------------------------------------------------*
*2011-11-17wanjjv1.0.0修改原因
*/

public static voidInt16ToByte2(Shorti, byte[]bts, intoffset){
byte[]btsTemp=Int16ToByte2(i);
intindex=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
*
*ModificationHistory:
*DateAuthorVersionDescription
*---------------------------------------------------------*
*2011-11-17wanjjv1.0.0修改原因
*/

public static byte[]UIntToByte4(LongVal){
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);
returnbts;
}

/**
*@Title:UIntToByte4
*@Description:TODO(将Uint32转化为byte4)
*
*
@param i
*
@param bts
*
@param offset
*
@return :void
*
*
@version :v1.0.0
*
@author :WANJJ
*@date:2011-11-17
*
*ModificationHistory:
*DateAuthorVersionDescription
*---------------------------------------------------------*
*2011-11-17wanjjv1.0.0修改原因
*/

public static voidUIntToByte4(Longi, byte[]bts, intoffset){
byte[]btsTemp=UIntToByte4(i);
intindex=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
*
*ModificationHistory:
*DateAuthorVersionDescription
*---------------------------------------------------------*
*2011-11-17wanjjv1.0.0修改原因
*/

public static byte[]IntToByte4(IntegerVal){
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);
returnbts;
}
/**
*@Title:IntToByte4
*@Description:TODO(将int32转化为byte4)
*
*
@param i
*
@param bts
*
@param offset
*
@return :void
*
*
@version :v1.0.0
*
@author :WANJJ
*@date:2011-11-17
*
*ModificationHistory:
*DateAuthorVersionDescription
*---------------------------------------------------------*
*2011-11-17wanjjv1.0.0修改原因
*/

public static voidIntToByte4(Integeri, byte[]bts, intoffset){
byte[]btsTemp=IntToByte4(i);
intindex=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
*
*ModificationHistory:
*DateAuthorVersionDescription
*---------------------------------------------------------*
*2011-11-17wanjjv1.0.0修改原因
*/

public static byte[]FloatToByte4( floatval){
returnIntToByte4(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
*
*ModificationHistory:
*DateAuthorVersionDescription
*---------------------------------------------------------*
*2011-11-17wanjjv1.0.0修改原因
*/

public static voidFloatToByte4( floatval, byte[]bts, intoffset){
byte[]btsTemp=FloatToByte4(val);
intindex=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
*
*ModificationHistory:DateAuthorVersionDescription
*---------------------------------------------------------*
*2011-11-8wanjjv1.0.0修改原因
*/

public static shortByte2ToInt16( bytebt1, bytebt2){
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
*
*ModificationHistory:DateAuthorVersionDescription
*---------------------------------------------------------*
*2011-11-8wanjjv1.0.0修改原因
*/

public static shortByte2ToInt16( byte[]bts, intoffset){
returnByte2ToInt16(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
*
*ModificationHistory:DateAuthorVersionDescription
*---------------------------------------------------------*
*2011-11-8wanjjv1.0.0修改原因
*/

public staticIntegerByte2ToUInt16( bytebt1, bytebt2){
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
*
*ModificationHistory:DateAuthorVersionDescription
*---------------------------------------------------------*
*2011-11-8wanjjv1.0.0修改原因
*/

public staticIntegerByte2ToUInt16( byte[]bts, intoffset){
returnByte2ToUInt16(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
*
*ModificationHistory:DateAuthorVersionDescription
*---------------------------------------------------------*
*2011-11-8wanjjv1.0.0修改原因
*/

public staticIntegerByte4ToInt32( bytebt1, bytebt2, bytebt3, bytebt4){
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
*
*ModificationHistory:DateAuthorVersionDescription
*---------------------------------------------------------*
*2011-11-8wanjjv1.0.0修改原因
*/

public staticIntegerByte4ToInt32( byte[]bts, intoffset){
returnByte4ToInt32(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
*
*ModificationHistory:DateAuthorVersionDescription
*---------------------------------------------------------*
*2011-11-8wanjjv1.0.0修改原因
*/

public static longByte4ToUInt32( bytebt1, bytebt2, bytebt3, bytebt4){

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
*
*ModificationHistory:DateAuthorVersionDescription
*---------------------------------------------------------*
*2011-11-8wanjjv1.0.0修改原因
*/

public static longByte4ToUInt32( byte[]bts, intoffset){
returnByte4ToUInt32(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
*
*ModificationHistory:DateAuthorVersionDescription
*---------------------------------------------------------*
*2011-11-8wanjjv1.0.0修改原因
*/

public static floatByte4ToFloat( bytebt1, bytebt2, bytebt3, bytebt4){
returnFloat.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
*
*ModificationHistory:DateAuthorVersionDescription
*---------------------------------------------------------*
*2011-11-8wanjjv1.0.0修改原因
*/

public static floatByte4ToFloat( byte[]bts, intoffset){
returnByte4ToFloat(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
*
*ModificationHistory:DateAuthorVersionDescription
*---------------------------------------------------------*
*2011-11-8wanjjv1.0.0修改原因
*/

public static byte[]StringToGBK(Stringstr)
throwsUnsupportedEncodingException{

returnstr.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
*
*ModificationHistory:DateAuthorVersionDescription
*---------------------------------------------------------*
*2011-11-8wanjjv1.0.0修改原因
*/

public static intFillSGBK(Stringstr, byte[]bts, intoffset)
throwsUnsupportedEncodingException{
returnFillSGBK(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
*
*ModificationHistory:DateAuthorVersionDescription
*---------------------------------------------------------*
*2011-11-8wanjjv1.0.0修改原因
*/

public static intFillSGBK(Stringstr, byte[]bts, intoffset, intlength)
throwsUnsupportedEncodingException{
intlen=0;
if(str!= null){
byte[]tmp=StringToGBK(str);
// 填充数据
for( inti=0;i<tmp.length;i++){
bts[offset+i]=tmp[i];
}
len=tmp.length;
}
// 补0
for( inti=len;i<length;i++){
bts[offset+i]=0;
}
returnlen>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
*
*ModificationHistory:DateAuthorVersionDescription
*---------------------------------------------------------*
*2011-11-8wanjjv1.0.0修改原因
*/

public staticStringGBKToString( byte[]bts)
throwsUnsupportedEncodingException{
returnGBKToString(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
*
*ModificationHistory:DateAuthorVersionDescription
*---------------------------------------------------------*
*2011-11-8wanjjv1.0.0修改原因
*/

public staticStringGBKToString( byte[]bts, intoffset)
throwsUnsupportedEncodingException{
returnSGBKToString(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
*
*ModificationHistory:DateAuthorVersionDescription
*---------------------------------------------------------*
*2011-11-8wanjjv1.0.0修改原因
*/

public staticStringSGBKToString( byte[]bts, intoffset, intcount)
throwsUnsupportedEncodingException{
return newString(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
*
*ModificationHistory:DateAuthorVersionDescription
*---------------------------------------------------------*
*2011-11-8wanjjv1.0.0修改原因
*/

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

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

public staticStringBytesToHexString( byte[]bts){
returnBytesToHexString(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
*
*ModificationHistory:DateAuthorVersionDescription
*---------------------------------------------------------*
*2011-11-8wanjjv1.0.0修改原因
*/

public staticStringBytesToHexString( byte[]bts, intoffset, intcount){
StringBuildersb= newStringBuilder(bts.length*2);
for( inti=0;i<count;i++){
sb.append(Integer.toHexString(bts[i+offset]));
}
returnsb.toString();
}

public static intBoolToByte( booleanbl){

returnbl?1:0<<0;
}
}


BCDHelper

packagecom.van.base;

importjava.io.UnsupportedEncodingException;

importandroid.text.format.Time;

public classBCDHelper{
/**
*@Title:StrToBCD
*@Description:TODO(用BCD码压缩数字字符串)
*
*
@param str
*
@return
*
@return :byte[]
*
*
@version :v1.0.0
*
@author :WANJJ
*@date:2011-11-17
*
*ModificationHistory:
*DateAuthorVersionDescription
*---------------------------------------------------------*
*2011-11-17wanjjv1.0.0修改原因
*/

public static byte[]StrToBCD(Stringstr){

returnStrToBCD(str,str.length());
}

public static byte[]StrToBCD(Stringstr, intnumlen){
if(numlen%2!=0)
numlen++;

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

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

intiTemp=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++;
}
returnbStr;

}

/**
*@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
*
*ModificationHistory:
*DateAuthorVersionDescription
*---------------------------------------------------------*
*2011-11-17wanjjv1.0.0修改原因
*/

public static intbcdToInt( byte[]bcdNum, intoffset, intnumlen){
returnInteger.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
*
*ModificationHistory:
*DateAuthorVersionDescription
*---------------------------------------------------------*
*2011-11-17wanjjv1.0.0修改原因
*/

public staticStringbcdToString( byte[]bcdNum, intoffset, intnumlen){
intlen=numlen/2;
if(numlen%2!=0)
len++;

StringBuffersb= newStringBuffer();
for( inti=0;i<len;i++){
sb.append(Integer.toHexString((bcdNum[i+offset]&0xf0)>>4));
sb.append(Integer.toHexString(bcdNum[i+offset]&0xf));
}
returnsb.toString();
}

/**
*@Title:Bcd3ToDateTime
*@Description:TODO(BCD码转日期)
*
*
@param data
*
@param offset
*
@return
*
@return :Time
*
*
@version :v1.0.0
*
@author :WANJJ
*@date:2011-11-17
*
*ModificationHistory:
*DateAuthorVersionDescription
*---------------------------------------------------------*
*2011-11-17wanjjv1.0.0修改原因
*/

public staticTimeBcd3ToDateTime( byte[]data, intoffset){
intyear=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);
Timetime= newTime("GMT+8");
time.set(monthDay,month,year);
returntime;
}

public staticTimeBcd5ToDateTime( byte[]data, intoffset){
intyear=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);

Timetime= newTime("GMT+8");
time.set(second,minute,hour,monthDay,month,year);
returntime;
}
/**
*@Title:Bcd6ToDateTime
*@Description:TODO(BCD码转时间格式)
*
*
@param data
*
@param offset
*
@return
*
@return :Time
*
*
@version :v1.0.0
*
@author :WANJJ
*@date:2011-11-17
*
*ModificationHistory:
*DateAuthorVersionDescription
*---------------------------------------------------------*
*2011-11-17wanjjv1.0.0修改原因
*/

public staticTimeBcd6ToDateTime( byte[]data, intoffset){
intyear=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);

Timetime= newTime("GMT+8");
time.set(second,minute,hour,monthDay,month,year);
returntime;
}

public staticTimeBcd7ToDateTime( byte[]data, intoffset){
intyear=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);

Timetime= newTime("GMT+8");
time.set(second,minute,hour,monthDay,month,year);
returntime;
}

/**
*@Title:DateTimeToBcd3
*@Description:TODO(日期转BCD码)
*
*
@param dt
*
@return
*
@throws UnsupportedEncodingException
*
@return :byte[]
*
*
@version :v1.0.0
*
@author :WANJJ
*@date:2011-11-17
*
*ModificationHistory:
*DateAuthorVersionDescription
*---------------------------------------------------------*
*2011-11-17wanjjv1.0.0修改原因
*/

public static byte[]DateTimeToBcd3(Timedt)
throwsUnsupportedEncodingException{

StringBuildersb= newStringBuilder();
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));

returnStrToBCD(sb.toString());
}

public static byte[]DateTimeToBcd5(Timedt)
throwsUnsupportedEncodingException{

StringBuildersb= newStringBuilder();

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));

returnStrToBCD(sb.toString());
}

public static byte[]DateTimeToBcd6(Timedt)
throwsUnsupportedEncodingException{
StringBuildersb= newStringBuilder();

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));

returnStrToBCD(sb.toString());
}

public static byte[]DateTimeToBcd7(Timedt)
throwsUnsupportedEncodingException{
StringBuildersb= newStringBuilder();
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));
returnStrToBCD(sb.toString());
}

public staticStringFStrLen(Stringstr, intlen){
if(str.length()<len){
str="0"+str;
}
returnstr;
}

/**
*@Title:GetNowTime
*@Description:TODO(获取当前GMT+8时间)
*
*
@return
*
@return :Time
*
*
@version :v1.0.0
*
@author :WANJJ
*@date:2011-11-9
*
*ModificationHistory:DateAuthorVersionDescription
*---------------------------------------------------------*
*2011-11-9wanjjv1.0.0修改原因
*/
public staticTimeGetNowTime(){

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

returnt;
}

public staticTimeGetNewTime(){
return newTime("GMT+8");
}

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

public static longTimeDiff(Timetm1,Timetm2){
return(tm1.toMillis( true)-tm2.toMillis( true));
}
}


CheckHelper

packagecom.van.base;

importjava.util.ArrayList;

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

/**
*得出CRC计算结果
*
*
@param buf
*要计算CRC的字符串
*
@return 整数
*/
public static intgetCRC(Stringbuf){
intcrc=0xFFFF; // initialvalue
intpolynomial=0x1021; // 0001000000100001(0,5,12)

for( intj=0;j<buf.length();j++){
charb=buf.charAt(j);
for( inti=0;i<8;i++){
booleanbit=((b>>(7-i)&1)==1);
booleanc15=((crc>>15&1)==1);
crc<<=1;
if(c15^bit)
crc^=polynomial;
}
}

crc&=0xffff;
returncrc;
}

/**
*得出CRC计算结果
*
*
@param buf
*要计算CRC的字符串
*
@return 字符串,2个字节
*/
public staticStringgetCRCString(Stringbuf){
intcrc=0xFFFF; // initialvalue
intpolynomial=0x1021; // 0001000000100001(0,5,12)

for( intj=0;j<buf.length();j++){
charb=buf.charAt(j);
for( inti=0;i<8;i++){
booleanbit=((b>>(7-i)&1)==1);
booleanc15=((crc>>15&1)==1);
crc<<=1;
if(c15^bit)
crc^=polynomial;
}
}

crc&=0xffff;
Stringstr=""+( char)(crc/256)+( char)(crc%256);
returnstr;
}

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

/**
*校验并转义(异或效验)
*
*
@param data
*要计算异或的数据
*
@param offset
*偏移位
*
@param length
*长度
*
@return byte
*/
public static voidXORAndEscape(ArrayList<Byte>data, intoffset, intlength){
bytexda=0;
for( inti=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);
}
}

更多相关文章

  1. Android 存储字符串数据到txt文件
  2. Android字符串进阶之三:字体属性及测量(FontMetrics)
  3. Android测量字符串所占UI的大小
  4. android 字符串转换成JSON对象
  5. Android中 字符串-数字 的转化
  6. Android 字符串转换大小写
  7. android 计算字符串长度,高度

随机推荐

  1. Android(安卓)RecyclerView +SnapHelper
  2. Android中ImageSpan的使用
  3. Eclipse下Android编程代码自动提示
  4. Android串口通信封装之OkUSB
  5. 【笔记】关于java.exe finished with non
  6. requires compiler compliance level 5.0
  7. SQLite数据库增删改查操作
  8. Unity3d获取persistentDataPath返回空的
  9. Fragment生命周期及基本使用
  10. Android腾讯微薄客户端开发六:给用户加VI