Work has such a demand before looking for a lot of no, finally have a can of hurriedly stay.
Original: Http://blog.163.com/[email protected]/blog/static/113561841201013525720/
string conversion to hexadecimal string Method 1:/*** String converted to hexadecimal string*/ Public Staticstring Str2hexstr (String str) {Char[] chars = "0123456789ABCDEF". ToCharArray (); StringBuilder SB=NewStringBuilder (""); byte[] bs =str.getbytes (); intbit; for(inti = 0; i < bs.length; i++) {bit= (Bs[i] & 0x0f0) >> 4; Sb.append (Chars[bit]); Bit= Bs[i] & 0x0f; Sb.append (Chars[bit]); } returnsb.tostring (); The hexadecimal string is converted into Array Method 1:/*** Convert 16 binary string to byte array *@paramhexstring *@returnbyte[]*/ Public Static byte[] hexstringtobyte (String hex) {intLen = (Hex.length ()/2); byte[] result =New byte[Len]; Char[] Achar =Hex.tochararray (); for(inti = 0; i < Len; i++) { intpos = i * 2; Result[i]= (byte) (ToByte (Achar[pos]) << 4 | tobyte (Achar[pos + 1])); } returnresult; } Private Static intToByte (Charc) {byteB = (byte) "0123456789ABCDEF". IndexOf (c); returnb;} Convert array to hexadecimal string Method 1:/*** Array converted to hexadecimal string *@parambyte[] *@returnhexstring*/ Public Static FinalString bytestohexstring (byte[] barray) {StringBuffer SB=NewStringBuffer (barray.length); String stemp; for(inti = 0; i < barray.length; i++) {stemp= Integer.tohexstring (0xFF &Barray[i]); if(Stemp.length () < 2) Sb.append (0); Sb.append (Stemp.touppercase ()); } returnsb.tostring ();}byte[] array into hexadecimal string method 2:/*** The array is converted to a hexadecimal string *@parambyte[] *@returnhexstring*/ Public StaticString ToHexString1 (byte[] b) {StringBuffer buffer=NewStringBuffer (); for(inti = 0; i < b.length; ++i) {Buffer.append (ToHexString1 (B[i])); } returnbuffer.tostring (); } Public StaticString ToHexString1 (byteb) {String s= Integer.tohexstring (b & 0xFF); if(s.length () = = 1){ return"0" +s; }Else{ returns; }} hexadecimal string conversion string Method 1:/*** Hexadecimal string converted to String *@paramhexstring *@returnString*/ Public Staticstring Hexstr2str (String hexstr) {string str= "0123456789ABCDEF"; Char[] Hexs =Hexstr.tochararray (); byte[] bytes =New byte[Hexstr.length ()/2]; intN; for(inti = 0; i < bytes.length; i++) {n= Str.indexof (hexs[2 * i]) * 16; N+ = Str.indexof (hexs[2 * i + 1]); Bytes[i]= (byte) (N & 0xFF); } return NewString (bytes); } hexadecimal string Conversion string Method 2:/*** Hexadecimal string conversion String *@paramhexstring *@returnString*/ Public Staticstring Tostringhex (string s) {byte[] Bakeyword =New byte[S.length ()/2]; for(inti = 0; i < bakeyword.length; i++) { Try{Bakeyword[i]= (byte) (0xFF &Integer.parseint (s.substring (i* 2, I * 2 + 2), 16)); } Catch(Exception e) {e.printstacktrace (); } } Try{s=NewString (Bakeyword, "utf-8");//Utf-16le:not}Catch(Exception E1) {e1.printstacktrace (); } returns;}
hexadecimal string converted to byte array