Conversion of Byte to 16 binary strings in Java

Source: Internet
Author: User

 * Convert byte[] to hex string. Here we can convert a byte to int, and then use integer.tohexstring (int) to convert it to a 16 binary string. * @param src byte[] Data * @return Hex String */public static string bytestohexstring (byte[] src) {Stringbui      Lder 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[] * @param hexstring the hex string * @return byte[] */public static byte[] Hexs      Tringtobytes (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 byte chartobyte (char c) {return (byte)  "0123456789ABCDEF". IndexOf (c);   }

  

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, 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, respectively. and combine to get the result of a byte conversion to a 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.

public static string bytes2hexstring (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;  }  

Here b[i] & 0xFF a byte and 0xFF are performed with the operation, and then using integer.tohexstring to obtain the hexadecimal string, you can see
b[i] & 0xFF operation is still an int, then why and 0xFF to do with the operation? Direct integer.tohexstring (b[i]); Would you like to convert byte strong to int? The answer is No.

The reason for this is:
The size of 1.byte is 8bits and the size of int is 32bits
The 2.java binary is in complement form

Byte is a byte saved with 8 bits, which is 8 0, 1.
The first bit of a 8-bit is the sign bit,
Which means that 0000 0001 represents the number 1.
1000 0000 means-1.
So the positive maximum is 0111 1111, which is the number 127.
Negative numbers up to 1111 1111, that is, numbers-128

The integer.tohexstring parameter is int, and if you do not &0xff, then when a byte is converted to int, because int is 32 bits, and byte is only 8 bits, then the complement is
For example, the decimal number of the complement 11111111 is 1 to int when converted to 11111111111111111111111111111111 many 1 ah, hehe! That is 0xFFFFFFFF but this number is wrong, and this complement will cause errors.
and 0xFF and after the high 24 bits will be cleared 0, the result is right.

1, anti-code:
If a number is positive, then its inverse code is the same as the original code;
If a number is negative, then the sign bit is 1, the rest of you are the original code to take the reverse;

2 complement

⑴ A number is positive, then its original code, anti-code, the same complement
⑵ a number is negative, just the sign bit is 1, the rest of you are the original code inversion, and then the entire number plus 1

Conversion of Byte to 16 binary strings in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.