Original URL: http://blog.csdn.net/caijunjun1006/article/details/11740223
In Java, Byte uses a binary representation of 8 bits, and we know that each character of the 16 binary needs to be represented by a 4-bit bits (23 + 22 + 21 + 20 = 15), so we can convert each byte to two corresponding 16 characters, That is, the high 4-bit and low 4 bits of byte are converted to the corresponding 16-character H and L, and combined to get the result of byte conversion to the 16 binary string, new string (H) + new String (L). That is, byte represents only 2 bits in hexadecimal notation.
In the same way, the opposite conversion also converts two 16 characters into a byte, as in the same principle.
Based on the above, we can convert the byte[] array to a 16 binary string, and of course we can convert the 16 binary string to the byte[] array.
Here is the tool class that implements conversion between byte[] arrays and hexadecimal strings:
[Java]View Plaincopy
- Package text.com;
- Public class Bytesutil {
- /**
- * Convert byte[] to hex string. Convert a byte array to a string
- * Here we can convert byte to int, then use integer.tohexstring (int) to convert to 16 binary string.
- * @param src byte[] Data
- * @return Hex string
- */
- public static String bytestohexstring (byte[] src) {
- StringBuilder StringBuilder = new StringBuilder ("");
- if (src = = Null | | src.length <= 0) {
- return null;
- }
- For (int i = 0; i < src.length; i++) {
- int v = src[i] & 0xFF;
- String HV = integer.tohexstring (v);
- if (Hv.length () < 2) {
- Stringbuilder.append (0);
- }
- Stringbuilder.append (hv+"");
- }
- return stringbuilder.tostring ();
- }
- /**
- * Convert hex string to byte[] converts a string to a byte array
- * @param hexstring the hex string
- * @return byte[]
- */
- public static byte[] Hexstringtobytes (String hexstring) {
- if (hexstring = = Null | | hexstring.equals ("")) {
- 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;
- }
- /**
- * Convert Char to Byte
- * @param c Char
- * @return Byte
- */
- private static byte chartobyte (char c) {
- return (byte) "0123456789ABCDEF". IndexOf (c);
- }
- }
Here is the test program:
[Java]View Plaincopy
- Package text.com;
- Public class Test {
- public static void Main (string[] args) {
- byte B1 = 11;
- byte b2 = 21;
- byte B3 = 31;
- byte b4 = 41;
- byte B5 = 51;
- byte B6 = 61;
- byte B7 = 71;
- byte B8 = 81;
- byte[] bytes = new byte[] {b1, b2, B3, B4, B5, B6, B7, B8};
- String hexstring = "0b 1f 3d 47 51";
- String str = bytesutil.bytestohexstring (bytes);
- SYSTEM.OUT.PRINTLN ("str--->" + str);
- byte[] hexstringtobytes = bytesutil.hexstringtobytes (hexstring);
- For (int i = 0; i < hexstringtobytes.length; i++) {
- System.out.println ("hexstringtobytes---->" + hexstringtobytes[i]);
- }
- }
- }
Console output:
STR--->0b 1f 3d 47 51
Hexstringtobytes---->11
Hexstringtobytes---->21
Hexstringtobytes---->31
Hexstringtobytes---->41
Hexstringtobytes---->51
Hexstringtobytes---->61
Hexstringtobytes---->71
Hexstringtobytes---->81
ok!
Conversion between "Go" javabyte[] array and hexadecimal string util------include case and code