Java byte to hex 16
PackageCom.longtop.client.codec.encryp; Public classHextransfer {/*** Converts a byte array to a string representing 16 binary values, such as: byte[]{8,18} to: 0813, and public static byte[] * Hexstr2bytearr (string strin) mutually reversible conversions Process * *@paramARRB * Byte array required for conversion *@returnconverted String *@throwsException * This method does not handle any exceptions, all exceptions are thrown*/ Public StaticString Bytearr2hexstr (byte[] ARRB) { intIlen =arrb.length; //each byte is represented by two characters, so the length of the string is twice times the length of the arrayStringBuffer SB =NewStringBuffer (Ilen * 2); for(inti = 0; i < Ilen; i++) { intInttmp =Arrb[i]; //convert negative numbers to positive numbers while(Inttmp < 0) {inttmp= Inttmp + 256; } //numbers less than 0F need to be preceded by 0 if(Inttmp < 16) {sb.append ("0"); } sb.append (Integer.tostring (inttmp,16)); } returnsb.tostring (); } /*** Converts a string representing 16 binary values to a byte array, and public static string Bytearr2hexstr (byte[] arrb) * Reciprocal conversion process * *@paramStrin * The string to convert *@returnconverted byte array *@throwsException * This method does not handle any exceptions, all exceptions are thrown *@author<a href= "Mailto:[email protected]" >LiGuoQing</a>*/ Public Static byte[] Hexstr2bytearr (String strin) {byte[] ARRB =strin.getbytes (); intIlen =arrb.length; //two characters represent one byte, so the length of the byte array is the length of the string divided by 2 byte[] Arrout =New byte[ILEN/2]; for(inti = 0; i < Ilen; i = i + 2) {String strtmp=NewString (ARRB, I, 2); Arrout[i/2] = (byte) Integer.parseint (strtmp, 16); } returnArrout; }}
Java byte to hex