The string is converted to 16 binary public static string tohexstring (string s) {string str = ""; for (int i = 0; I < s.length (); i++) {int ch = (int) s.charat (i); String s4 = integer.tohexstring (ch); str = str + s4;} Return "0x" + str;//0x represents the hexadecimal}//convert hexadecimal encoding to a string public static string Tostringhex (string s) {if ("0x"). Equals (s.substring (0, 2) ) {s = s.substring (2);} byte[] Bakeyword = new Byte[s.length ()/2];for (int i = 0; i < bakeyword.length; i++) {try {bakeyword[i] = (byte) (0xf F & Integer.parseint (S.substring (i * 2, I * 2 + 2), 16));} catch (Exception e) {e.printstacktrace ();}} try {s = new String (Bakeyword, "Utf-8"),//Utf-16le:not} catch (Exception E1) {e1.printstacktrace ();} return s;} The method of converting 16 binary numbers to 16 strings public static string inthexstring (byte[] b) {string ret = ""; for (int i = 0; i < b.length; i++) {String hex = integer.tohexstring (B[i] & 0xFF), if (hex.length () = = 1) {hex = ' 0 ' + hex;} RET + = Hex.touppercase ();} return ret;}
/* * String converted to 16 binary byte array */public static byte[] Hexstringtobytes (String hexstring) {if (hexstring = null | | hexstring.equ ALS ("")) {return null;} hexstring = Hexstring.touppercase (); int length = Hexstring.length ()/2;char[] Hexchars = Hexstring.tochararray (); byte[] D = new Byte[length];for (int i = 0; i < length; i++) {int pos = i * 2;d[i] = (byte) (Chartobyte (Hexchars[pos]) << ; 4 | Chartobyte (Hexchars[pos + 1]));} return D;} public static byte Chartobyte (char c) {return (byte) "0123456789ABCDEF". IndexOf (c); /* * received 16 binary bytes[] converted to string */public static string bytestohexstring (byte[] b) {String ret = ""; for (int i = 0; i < b.length; i++) {String hex = integer.tohexstring (B[i] & 0xFF), if (hex.length () = = 1) {hex = ' 0 ' + hex;} RET + = Hex.touppercase ();} return ret;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Various conversion issues related to 16 binary