publicclassByteUtil {/*** Convert a String of hexadecimal digits into the corresponding byte array
* by encoding each two hexadecimal digits as a byte.
*
*@paramdigits
* Hexadecimal digits representation
*
*@exceptionIllegalArgumentException
* if an invalid hexadecimal digit is found, or the input
* string contains an odd number of hexadecimal digits*/publicstaticbyte[] hexStringToByteArray(String digits) {
ByteArrayOutputStream baos=newByteArrayOutputStream();for(inti=0; i=digits.length()) {thrownewIllegalArgumentException("hexUtil.odd");
}charc2=digits.charAt(i+1);byteb=0;if((c1>='0')&&(c1<='9'))
b+=((c1-'0')*16);elseif((c1>='a')&&(c1<='f'))
b+=((c1-'a'+10)*16);elseif((c1>='A')&&(c1<='F'))
b+=((c1-'A'+10)*16);elsethrownewIllegalArgumentException("hexUtil.bad");if((c2>='0')&&(c2<='9'))
b+=(c2-'0');elseif((c2>='a')&&(c2<='f'))
b+=(c2-'a'+10);elseif((c2>='A')&&(c2<='F'))
b+=(c2-'A'+10);elsethrownewIllegalArgumentException("hexUtil.bad");
baos.write(b);
}return(baos.toByteArray());
}/*** Convert a byte array into a printable format containing a String of
* hexadecimal digit characters (two per byte).
*@seethis method consume too much memory, especailly when large byte array.
*
*@parambytes
* Byte array representation*/publicstaticString convertByteArrayToHexStr(bytebytes[]) {
StringBuffer sb=newStringBuffer(bytes.length*2);for(inti=0; i
sb.append(convertDigitToHexChar((int) (bytes[i]>>4)));
sb.append(convertDigitToHexChar((int) (bytes[i]&0x0f)));
}return(sb.toString());
}/*** convert byte array into hex string, with , as seperator
*@seethis method consume too much memory, especailly when large byte array.
*@parambytes
*@return*/publicstaticString convertByteArrayToHexStr2(bytebytes[]) {
StringBuffer sb=newStringBuffer();for(inti=0; i
sb.append(convertDigitToHexChar((int) (bytes[i]>>4)));
sb.append(convertDigitToHexChar((int) (bytes[i]&0x0f)));
sb.append(",");
}return(sb.toString().substring(0,sb.length()-1));
}/*** Convert the specified value (0 .. 15, i.e. 4bits) to the corresponding hexadecimal
* digit.
*
*@paramvalue
* Value to be converted*/privatestaticcharconvertDigitToHexChar(intvalue) {
value=value&0x0f;if(value>=10)return((char) (value-10+'a'));elsereturn((char) (value+'0'));
}publicstaticString convertByteToHexStr(bytevalue) {
StringBuffer sb=newStringBuffer();
sb.append(convertDigitToHexChar((int) (value>>4)));
sb.append(convertDigitToHexChar((int) (value&0x0f)));return(sb.toString());
}/*** Convert an int to a byte array
*
*@paramvalue int
*@returnbyte[]*/publicstaticbyte[] intToByteArray(intvalue) {byte[] b=newbyte[4];for(inti=0; i<4; i++) {intoffset=(b.length-1-i)*8;
b[i]=(byte) ((value>>offset)&0xFF);//}returnb;
}/*** change byte array into a unsigned byte array
*@paramsource
*@return*/publicstaticbyte[] toUnsignedByteArray(byte[] source){//ByteUtil.printByteArray(source);//to keep value in the 0-255intmodel=256;if(source==null||source.length==0){returnnewbyte[0];
}byte[] dest=newbyte[source.length];for(inti=0;i
dest[i]=(byte)tmp;
}returndest;
}/*** Convert the byte array to an int starting from the given offset.
*
*@paramb
* The byte array
*@paramoffset
*
*@returnThe integer*/publicstaticintbyteArrayToInt(byte[] b) {if(b.length>4){thrownewRuntimeException("more than 4 byte");
}intvalue=0;for(inti=0; i
value+=(b[i]&0xFF)<
}returnvalue;
}
}