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

Mar 08, 2017 am 10:21 AM

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)!


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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Square Root in Java Square Root in Java Aug 30, 2024 pm 04:26 PM

Guide to Square Root in Java. Here we discuss how Square Root works in Java with example and its code implementation respectively.

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Random Number Generator in Java Random Number Generator in Java Aug 30, 2024 pm 04:27 PM

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Armstrong Number in Java Armstrong Number in Java Aug 30, 2024 pm 04:26 PM

Guide to the Armstrong Number in Java. Here we discuss an introduction to Armstrong's number in java along with some of the code.

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

See all articles