byte to int occurrence negative condition __ Binary

Source: Internet
Author: User

Problem Description:

When a file stream reads a hexadecimal file, it is often necessary to read some bytes out to parse the INT data that represents the size, and there may be negative numbers in the conversion process, and the good solution is to byte[i and 0xFF, because Byte is 8bit and int is 32bit, Therefore, in the transformation before the high 24 bits should be zero, so there will be no complement caused by the conversion error.


See the following code before you dissect the problem
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;
}
Above is a string that converts byte[] to hexadecimal, note that here b[I & 0xFF a byte and 0xFF and then use integer.tohexstring to get a hexadecimal string that shows
b[i] & 0xFF is still an int, so why and 0xFF? Direct integer.tohexstring (b[i)); Do you want to convert the byte to int? The answer is No.


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


I'm going to brush up on the basics of computer science here


Byte is saved in one byte and has 8 bits, that is, 8 0, 1.
The first bit of the 8-bit is the sign bit,
Which means 0000 0001 is the number 1.
1000 0000 Represents--1.
So the maximum number of digits is 0111 1111, which is 127.
The negative number is 1111 1111, which is the figure-128.


The above is the binary source code, but in Java is the form of complement, the following describes what is the complement


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


2, complement: the use of overflow, we can subtract into addition
For decimal numbers, 5 can be subtracted from 9:
9-4=5 because of 4+6=10, we can use 6 as a complement of 4.
Rewrite to addition:
9+6=15 (Remove the high 1, which is minus 10) to get 5.


For hexadecimal numbers, you can subtract from C through 5:
C-7=5 because 7+9=16 9 as the complement of 7
Rewrite to addition:
C+9=15 (Remove the high 1, which is minus 16) to get 5.


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


⑴ A number is positive, its original code, inverse code, complement the same
⑵ a number is negative, the symbol bit is 1, the rest of you are against the original code, and then the whole number plus 1


-1 of the original code is 10000001
-1 of the counter code is 11111110
+ 1
-1 's complement is 11111111


0 of the original code is 00000000
0 has an inverse code of 11111111 (the same inverse code as positive zero and minus zero)
+1
0 of the complement is 100000000 (give off the beginning of 1, positive zero and minus zero complement the same)


The integer.tohexstring argument is int, and if &0xff is not performed, then when a byte is converted to int, the int is 32 bits, and byte is only 8 digits, then the complement is made.
For example, when the decimal number of the complement 11111111 is 1 to int, it becomes 11111111111111111111111111111111 many 1, hehe. That is, 0xFFFFFFFF, but this number is not correct, such a complement will cause errors.
and 0xFF phase, high 24 bit will be cleared 0, the result is right.


----
A byte in Java whose range is -128~127, and the integer.tohexstring argument is an int, and if not &0xff, then when a byte is converted to int, a bit extension is done for negative numbers, for example, A byte of 1 (i.e. 0xff) is converted to 1 (i.e. 0xFFFFFFFF) of int, and the result is not what we want.


And 0xFF is the default, so, a byte and 0xff to participate in the first byte into a plastic operation, so that the result of the high 24 bits will always be clear 0, so the result is always what we want.

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.