分享一个 常用的 hex 转 characters或integer
这个如果不熟的话还是要查找许多资料和时间来解决这个问题的,下面分享这样一个类。
这样方便大家节省许多功夫去查找资料。
代码:
packageorg.apache.commons.codec.binary;importorg.apache.commons.codec.BinaryDecoder;importorg.apache.commons.codec.BinaryEncoder;importorg.apache.commons.codec.DecoderException;importorg.apache.commons.codec.EncoderException;/***Hexencoderanddecoder.**/publicclassHeximplementsBinaryEncoder,BinaryDecoder{/***UsedbuildingoutputasHex*/privatestaticfinalchar[]DIGITS={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};/***Convertsanarrayofcharactersrepresentinghexidecimalvaluesintoan*arrayofbytesofthosesamevalues.Thereturnedarraywillbehalfthe*lengthofthepassedarray,asittakestwocharacterstorepresentany*givenbyte.Anexceptionisthrownifthepassedchararrayhasanodd*numberofelements.**@paramdataAnarrayofcharacterscontaininghexidecimaldigits*@returnAbytearraycontainingbinarydatadecodedfrom*thesuppliedchararray.*@throwsDecoderExceptionThrownifanoddnumberorillegalofcharacters*issupplied*/publicstaticbyte[]decodeHex(char[]data)throwsDecoderException{intlen=data.length;if((len&0x01)!=0){thrownewDecoderException("Oddnumberofcharacters.");}byte[]out=newbyte[len>>1];//twocharactersformthehexvalue.for(inti=0,j=0;j<len;i++){intf=toDigit(data[j],j)<<4;j++;f=f|toDigit(data[j],j);j++;out[i]=(byte)(f&0xFF);}returnout;}/***Convertsahexadecimalcharactertoaninteger.**@paramchAcharactertoconverttoanintegerdigit*@paramindexTheindexofthecharacterinthesource*@returnAninteger*@throwsDecoderExceptionThrownifchisanillegalhexcharacter*/protectedstaticinttoDigit(charch,intindex)throwsDecoderException{intdigit=Character.digit(ch,16);if(digit==-1){thrownewDecoderException("Illegalhexadecimalcharcter"+ch+"atindex"+index);}returndigit;}/***Convertsanarrayofbytesintoanarrayofcharactersrepresentingthehexidecimalvaluesofeachbyteinorder.*Thereturnedarraywillbedoublethelengthofthepassedarray,asittakestwocharacterstorepresentany*givenbyte.**@paramdata*abyte[]toconverttoHexcharacters*@returnAchar[]containinghexidecimalcharacters*/publicstaticchar[]encodeHex(byte[]data){intl=data.length;char[]out=newchar[l<<1];//twocharactersformthehexvalue.for(inti=0,j=0;i<l;i++){out[j++]=DIGITS[(0xF0&data[i])>>>4];out[j++]=DIGITS[0x0F&data[i]];}returnout;}/***Convertsanarrayofcharacterbytesrepresentinghexidecimalvaluesintoan*arrayofbytesofthosesamevalues.Thereturnedarraywillbehalfthe*lengthofthepassedarray,asittakestwocharacterstorepresentany*givenbyte.Anexceptionisthrownifthepassedchararrayhasanodd*numberofelements.**@paramarrayAnarrayofcharacterbytescontaininghexidecimaldigits*@returnAbytearraycontainingbinarydatadecodedfrom*thesuppliedbytearray(representingcharacters).*@throwsDecoderExceptionThrownifanoddnumberofcharactersissupplied*tothisfunction*@see#decodeHex(char[])*/publicbyte[]decode(byte[]array)throwsDecoderException{returndecodeHex(newString(array).toCharArray());}/***ConvertsaStringoranarrayofcharacterbytesrepresentinghexidecimalvaluesintoan*arrayofbytesofthosesamevalues.Thereturnedarraywillbehalfthe*lengthofthepassedStringorarray,asittakestwocharacterstorepresentany*givenbyte.Anexceptionisthrownifthepassedchararrayhasanodd*numberofelements.**@paramobjectAStringor,anarrayofcharacterbytescontaininghexidecimaldigits*@returnAbytearraycontainingbinarydatadecodedfrom*thesuppliedbytearray(representingcharacters).*@throwsDecoderExceptionThrownifanoddnumberofcharactersissupplied*tothisfunctionortheobjectisnotaStringorchar[]*@see#decodeHex(char[])*/publicObjectdecode(Objectobject)throwsDecoderException{try{char[]charArray=objectinstanceofString?((String)object).toCharArray():(char[])object;returndecodeHex(charArray);}catch(ClassCastExceptione){thrownewDecoderException(e.getMessage());}}/***Convertsanarrayofbytesintoanarrayofbytesforthecharactersrepresentingthe*hexidecimalvaluesofeachbyteinorder.Thereturnedarraywillbe*doublethelengthofthepassedarray,asittakestwocharactersto*representanygivenbyte.**@paramarrayabyte[]toconverttoHexcharacters*@returnAbyte[]containingthebytesofthehexidecimalcharacters*@see#encodeHex(byte[])*/publicbyte[]encode(byte[]array){returnnewString(encodeHex(array)).getBytes();}/***ConvertsaStringoranarrayofbytesintoanarrayofcharactersrepresentingthe*hexidecimalvaluesofeachbyteinorder.Thereturnedarraywillbe*doublethelengthofthepassedStringorarray,asittakestwocharactersto*representanygivenbyte.**@paramobjectaString,orbyte[]toconverttoHexcharacters*@returnAchar[]containinghexidecimalcharacters*@throwsEncoderExceptionThrownifthegivenobjectisnotaStringorbyte[]*@see#encodeHex(byte[])*/publicObjectencode(Objectobject)throwsEncoderException{try{byte[]byteArray=objectinstanceofString?((String)object).getBytes():(byte[])object;returnencodeHex(byteArray);}catch(ClassCastExceptione){thrownewEncoderException(e.getMessage());}}}
更多的移动互联网的发展趋势、app开发、移动互联网应用相关的资料请到互联网的一点事:www.yidin.net留言
android QQ群:222392467
资料:
http://www.yidin.net/?p=8280
http://www.yidin.net/?p=9725
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。