Conversion of Byte to 16 binary strings in Java

Source: Internet
Author: User

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.

/*convert byte[] to hex string. Here we can convert 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 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 (); }   /*** Convert hex string to byte[] *@paramhexstring the hex string *@returnbyte[]*/   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; }   /*** Convert Char to BYTE *@paramC char *@returnbyte*/   Private byteChartobyte (Charc) {return(byte) "0123456789ABCDEF". IndexOf (c); }  //prints the specified byte array to the console in 16-binary form Public Static voidPrinthexstring (byte[] b) { for(inti = 0; i < b.length; i++) {String hex= Integer.tohexstring (B[i] & 0xFF); if(hex.length () = = 1) {hex= ' 0 ' +Hex;       } System.out.print (Hex.touppercase ()); }      }  

Why byte in Java converts int to 0xFF and operation

Look at the following code before you dissect the problem

 public  static  String Bytes2hexstring (byte  [] b) {String ret  = "     ";  for  (int  i = 0; i < b.length; i++< Span style= "color: #000000;"      ) {String Hex  = integer.tohexstring (b[i] & 0xff);  if  (hex.length () = = 1) {Hex  = ' 0 '      + hex;     } ret  += Hex.touppercase ();    return   ret; }  

Above is the byte[] conversion hex String, note here b[i] & 0xFF will be a byte and 0xFF with the operation, and then use integer.tohexstring to obtain a 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

First, we will review the basic theory of computer

BYTE (byte)

Bit (bit)



A bit can only have a value, not 0 is 1, so if I give you a value of 0, you can say that it is a bit, if I give you two (00), you can say that they are two bits.

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

This is the binary source code, but in Java is used in the form of complement, the following describes what is the complement

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: Using overflow, we can change the subtraction into addition
For decimal numbers, 5 available subtraction is obtained from 9:
9-4=5 because 4+6=10, we can add 6 as a 4 complement.
Rewrite for addition:
9+6=15 (minus the high 1, which is 10) gets 5.

For hexadecimal numbers, the subtraction from C to 5 is available:
C-7=5 because 7+9=16 will be 9 as a complement of 7
Rewrite for addition:
C+9=15 (minus the high 1, which is 16) gets 5.

In the computer, if we use 1 bytes to represent a number, one byte has 8 bits, more than 8 bits go into 1, in memory the case is (100000000), carry 1 is discarded.

⑴ 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

-1 of the original code is 10000001
-1 of the anti-code is 11111110
+ 1
-1 of the complement is 11111111

0 of the original code is 00000000
0 of the anti-code is 11111111 (positive zero and negative zero of the same code)
+1
0 of the complement is 100000000 (drop the beginning of 1, positive zero and the same complement of negative zero)

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.

----
A byte in Java, whose range is -128~127, and the integer.tohexstring parameter is int, and if it is not &0xff, then when a byte is converted to int, for negative numbers, a bit extension is done, for example, A byte-1 (or 0xff) will be converted to INT-1 (that is, 0xFFFFFFFF), then the result of conversion is not what we want.

And 0xFF is the default is shaping, so, a byte with 0xFF to join the first to convert that byte into a shaping operation, so that the result of the high 24 bits will always be cleared 0, so the result is always what we want.

Java Number & 0xFF What's the explanation

Http://zhidao.baidu.com/question/286930743.html

Convert number to binary, taking the lowest 8 bits (bit). Because 0xFF binary is 1111 1111.
& Operation is, if the corresponding two bit is 1, then the bit result is 1, otherwise 0.
such as 1010 & 1101 = 1000 (binary)
Since the 0xFF lowest 8 bit is 1, so the number in the lower 8 bit of & After the original is 1, the result is 1, the original is 0, the result bit or 0. higher than 8, 0xFF is 0, so either 0 or 1, the result is 0.
Number if 0XABCD, then number & 0xff = number & 0x00ff = 0X00CD = 0xCD

Conversion of Byte to 16 binary strings in Java (RPM)

Related Article

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.