1 ).
A (-| +) 256 = C;
If a is positive, use a-256 = C. If a is negative, use a + 256 = C.
If the result C is not in-128 ~ Between 127, use C (-| +) 256 and so on
Result c is in-128 ~ Examples between 127:
Int number = 200;
200-256 =-56 byte Result 56 is in-128 ~ Between 127
Number =-200;
-200 + 256 = 200 byte result-56 in-128 ~ Between 127
Number = 500;
500-256 = 244 244 not in-128 ~ Between 127
244-256 =-12-12 in-128 ~ Between 127
2 ).
Example: int number = 500;
First convert the number to binary and then the last eight digits
If the maximum number of digits is not equal to 1, convert the obtained binary data.
The result is obtained in decimal format. If the maximum number of digits is 1
That uses this formula to calculate the decimal number of the binary number.
The converted byte result is obtained from the decimal number-256:
The same is true for the hexadecimal or octal format!
Example:
Number = 500
Because Int Is 32 bits and byte is 8 bits
500 binary: 0000 0000 0000 0000 0000 0001 1111 0100
Take the last eight bits: 1111 0100 and convert it to 244 in decimal format (the highest bits here is equal to 1)
244-256 =-12 so convert 500 to byte as-12
Number = 300
200 binary: 0000 0000 0000 0000 0000 0001 0010 1100
Take the last eight bits: 0010 1100 and convert it to decimal (the highest bits here is not equal to 1 ).
Result: 44
Http://j2ee-yohn.javaeye.com/blog/269734