ImportJavax.xml.bind.DatatypeConverter;Importjava.io.UnsupportedEncodingException; Public classTest { Public Static voidMain (string[] args)throwsunsupportedencodingexception{//Print Hex string version of HELLO World byte[] hellobytes = "HELLO World". GetBytes (); String Hellohex=datatypeconverter.printhexbinary (hellobytes); System.out.printf ("Hello hex:0x%s\n", Hellohex); //convert hex-encoded string back to original string byte[] Decodedhex =datatypeconverter.parsehexbinary (Hellohex); String decodedstring=NewString (Decodedhex, "UTF-8"); System.out.printf ("Hello Decoded:%s\n", decodedstring); }}
Final protected Static Char[] Hexarray = "0123456789ABCDEF". ToCharArray (); Public StaticString Bytestohex (byte[] bytes) { Char[] Hexchars =New Char[Bytes.length * 2]; for(intj = 0; J < Bytes.length; J + + ) { intv = bytes[j] & 0xFF; Hexchars[j* 2] = hexarray[v >>> 4]; Hexchars[j* 2 + 1] = hexarray[v & 0x0F]; } return NewString (hexchars);}
private static final char [] Hexcode = "0123456789ABCDEF" .tochararray (); public String printhexbinary (byte [] data) {StringBuilder R = new StringBuilder (data.length * 2); for (byte B:data) {R.append (hexcode[(b >> 4) & 0xf & 0xf return r.tostring (); }
Transferred from: http://blog.csdn.net/x_iya/article/details/54136799
[Go] Java byte[] Hex print