Home > Java > javaTutorial > Detailed introduction to code examples of Java byte array type (byte[]) and int type mutual conversion methods

Detailed introduction to code examples of Java byte array type (byte[]) and int type mutual conversion methods

黄舟
Release: 2017-03-08 10:21:53
Original
1765 people have browsed it

The following editor will bring you an article on how to convert Java byte array type (byte[]) and int type. The editor thinks it is quite good, so I will share it with you now and give it as a reference for everyone. Let’s follow the editor and take a look.

The code is as follows:


##

public class CommonUtils {
 //高位在前,低位在后
 public static byte[] int2bytes(int num){
 byte[] result = new byte[4];
 result[0] = (byte)((num >>> 24) & 0xff);//说明一
 result[1] = (byte)((num >>> 16)& 0xff );
 result[2] = (byte)((num >>> 8) & 0xff );
 result[3] = (byte)((num >>> 0) & 0xff );
 return result;
 }
 
 //高位在前,低位在后
 public static int bytes2int(byte[] bytes){
 int result = 0;
 if(bytes.length == 4){
  int a = (bytes[0] & 0xff) << 24;//说明二
  int b = (bytes[1] & 0xff) << 16;
  int c = (bytes[2] & 0xff) << 8;
  int d = (bytes[3] & 0xff);
  result = a | b | c | d;
 }
 return result;
 }
 
 public static void main(String[] args){
 int a = -64;
 System.out.println("-64="+Integer.toBinaryString(-64));
 byte[] bytes = CommonUtils.int2bytes(a);
 for(int i = 0 ; i<4 ; i++){
  System.out.println(bytes[i]);
 }
 a = CommonUtils.bytes2int(bytes);
 System.out.println(a);
 
 }
}
Copy after login


The running results are as follows:

##

-64=11111111111111111111111111000000
-1
-1
-1
-64
-64
Copy after login


Explanation 1: -64 is converted into binary original code as [10000000][00000000][00000000][01000000]

Convert the original code into complement code as [11111111][11111111] [11111111][11000000], which is the same as the console output. You can see that in

java, binary is expressed in two’s complement form

-64 >>> after 24 ( Unsigned right shift, high bits filled with 0), becomes [00000000][00000000][00000000][11111111]

After adding the result of the previous step & 0xff, it is still [00000000][00000000][00000000][ 11111111], since the value of 0xff is [00000000][00000000][00000000][11111111], the purpose of & 0xff is to keep the lowest 8 bits unchanged, and the remaining positions are 0

Then force the result into a byte type, retain the low bits, truncate the high bits, and become [11111111]. It can be seen that The 0xff in the previous step is actually unnecessary. No matter what the high bits are, they will eventually be truncated.

So result[0] is [11111111]=-1

and so on:

result[1]为[11111111]=-1
result[2]为[11111111]=-1
result[3]为[11000000]=-64
Copy after login

Explanation 2:

byte[0] is [11111111]. First, byte[0] will be converted to int type (Before the bit shift operation, the byte type will be converted to int type. If it is positive number, the high bits are filled with 0, if it is a negative number, the high bits are filled with 1

), the high bits are filled with 1, and it becomes [11111111][11111111][11111111][11111111]

Change the result of the previous step& After 0xff, it will become [00000000][00000000][00000000][11111111]

Then the result of the previous step is << 24 (left shift, low-order padding with 0), and it will become [ 11111111][00000000][00000000][00000000] = a

Similarly obtain b, c, d

finally a | b | c | d, that is:

[11111111][00000000][00000000][00000000] | 
[00000000][11111111][00000000][00000000] | 由于<<16位之前& 0xff,故保证b的最高8位都为0
[00000000][00000000][11111111][00000000] | 由于<<8位之前& 0xff,故保证c的最高16位都为0
[00000000][00000000][00000000][11000000] 由于& 0xff,故保证d的最高24为都为0
=[11111111][11111111][11111111][11000000] = -64
Copy after login

You can see In order to ensure that byte is converted into int, the padding will not affect the final result of a | b | c | d (set to 0),

& 0xff is necessary

short and byte The conversion between [] and the conversion between long and byte[] are also similar

PS:

1, int type accounts for 4 bytes, and the byte type only occupies 1 byte

2, original code: the highest bit is the sign bit, and the remaining bits are used to indicate the numerical size

2 Original code: 00000010 The original code of

-2: 10000010

3, inverse code:

The inverse code of a positive number is the same as its original code;

The sign bit of the inverse code of a negative number remains No change, the remaining bits are bitwise inverted The complement of 2: 00000010 The complement of

-2: 11111101

4, two's complement: the complement of a positive number is the same as its original code; the complement of a negative number is the complement of the negative number + 1

2's complement: 00000010

-2's complement: 11111110

The above is a detailed introduction to the code examples of Java byte array type (byte[]) and int type mutual conversion methods. For more related content, please pay attention to the PHP Chinese website (www .php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template