Original: http://blog.sina.com.cn/s/blog_62e9ec530101ebv6.html
Hexstring-->byte
Public Static byte[] hexstringtobytes (String hexstring) {if(HexString = =NULL|| Hexstring.equals ("")) { return NULL; } hexstring=hexstring.touppercase (); intLength = Hexstring.length ()/2; Char[] Hexchars =Hexstring.tochararray (); byte[] D =New byte[length]; for(inti = 0; i < length; i++) { intpos = i * 2; D[i]= (byte) (Chartobyte (Hexchars[pos]) << 4 | chartobyte (Hexchars[pos + 1])); } returnD; } Private Static byteChartobyte (Charc) {return(byte) "0123456789ABCDEF". IndexOf (c); }
byte-->string
Public StaticString bytestohexstring (byte[] src) {StringBuilder StringBuilder=NewStringBuilder (""); if(src = =NULL|| Src.length <= 0) { return NULL; } for(inti = 0; i < src.length; i++) { intv = src[i] & 0xFF; String HV=integer.tohexstring (v); if(Hv.length () < 2) {stringbuilder.append (0); } stringbuilder.append (HV); } returnstringbuilder.tostring (); }
byte-->hexstring
Public byte = ""; for (int i = 0; i < b.length; i++) { = integer.tohexstring (b[i] & 0xFF); if (hex.length () = = 1) {= ' 0 ' + hex; } = a +hex ; } return A;}
Conversion of string byte hexstring in Java