如何在Java中将int转换为三个字节?

时间:2022-07-30 16:57:31

I am trying to convert an int into three bytes representing that int (big endian).

我试图将一个int转换为三个字节,表示int(big endian)。

I'm sure it has something to do with bit-wise and and bit shifting. But I have no idea how to go about doing it.

我确定它与逐位和位移有关。但我不知道如何去做。

For example:

int myInt;

// some code

byte b1, b2 , b3; // b1 is most significant, then b2 then b3.

*Note, I am aware that an int is 4 bytes and the three bytes have a chance of over/underflowing.

*注意,我知道int是4个字节,三个字节有可能过度/不足。

4 个解决方案

#1


14  

To get the least significant byte:

要获得最不重要的字节:

b3 = myInt & 0xFF;

The 2nd least significant byte:

第二个最低有效字节:

b2 = (myInt >> 8) & 0xFF;

And the 3rd least significant byte:

第三个最低有效字节:

b1 = (myInt >> 16) & 0xFF;

Explanation:

Bitwise ANDing a value with 0xFF (11111111 in binary) will return the least significant 8 bits (bits 0 to 7) in that number. Shifting the number to the right 8 times puts bits 8 to 15 into bit positions 0 to 7 so ANDing with 0xFF will return the second byte. Similarly, shifting the number to the right 16 times puts bits 16 to 23 into bit positions 0 to 7 so ANDing with 0xFF returns the 3rd byte.

使用0xFF(二进制11111111)对值进行按位与运算将返回该数字中的最低有效8位(位0到7)。将数字向右移动8次将位8到15放入位0到7,因此与0xFF进行AND运算将返回第二个字节。类似地,将数字向右移16次将位16到23放入位0到7,因此与0xFF进行AND运算会返回第3个字节。

#2


4  

byte b1 = (myint >> 16) & 0xff;
byte b2 = (myint >> 8) & 0xff;
byte b3 = myint & 0xff;

I am unsure how this holfds in java though, i aam not a java dev

我不确定这在java中如何,但我不是一个java dev

#3


2  

An int doesn't fit into 3 bytes. However, assuming that you know these particular ones do:

int不适合3个字节。但是,假设您知道这些特定的:

   byte b1 = (myInt & 0xff);
   myInt >>= 8;
   byte b2 = (myInt & 0xff);
   myInt >>= 8;
   byte b3 = (myInt & 0xff);

#4


1  

In Java

int myInt = 1;
byte b1,b2,b3;
b3 = (byte)(myInt & 0xFF);
b2 = (byte)((myInt >> 8) & 0xFF);
b1 = (byte)((myInt >> 16) & 0xFF);
System.out.println(b1+" "+b2+" "+b3);

outputs 0 0 1

输出0 0 1

#1


14  

To get the least significant byte:

要获得最不重要的字节:

b3 = myInt & 0xFF;

The 2nd least significant byte:

第二个最低有效字节:

b2 = (myInt >> 8) & 0xFF;

And the 3rd least significant byte:

第三个最低有效字节:

b1 = (myInt >> 16) & 0xFF;

Explanation:

Bitwise ANDing a value with 0xFF (11111111 in binary) will return the least significant 8 bits (bits 0 to 7) in that number. Shifting the number to the right 8 times puts bits 8 to 15 into bit positions 0 to 7 so ANDing with 0xFF will return the second byte. Similarly, shifting the number to the right 16 times puts bits 16 to 23 into bit positions 0 to 7 so ANDing with 0xFF returns the 3rd byte.

使用0xFF(二进制11111111)对值进行按位与运算将返回该数字中的最低有效8位(位0到7)。将数字向右移动8次将位8到15放入位0到7,因此与0xFF进行AND运算将返回第二个字节。类似地,将数字向右移16次将位16到23放入位0到7,因此与0xFF进行AND运算会返回第3个字节。

#2


4  

byte b1 = (myint >> 16) & 0xff;
byte b2 = (myint >> 8) & 0xff;
byte b3 = myint & 0xff;

I am unsure how this holfds in java though, i aam not a java dev

我不确定这在java中如何,但我不是一个java dev

#3


2  

An int doesn't fit into 3 bytes. However, assuming that you know these particular ones do:

int不适合3个字节。但是,假设您知道这些特定的:

   byte b1 = (myInt & 0xff);
   myInt >>= 8;
   byte b2 = (myInt & 0xff);
   myInt >>= 8;
   byte b3 = (myInt & 0xff);

#4


1  

In Java

int myInt = 1;
byte b1,b2,b3;
b3 = (byte)(myInt & 0xFF);
b2 = (byte)((myInt >> 8) & 0xFF);
b1 = (byte)((myInt >> 16) & 0xFF);
System.out.println(b1+" "+b2+" "+b3);

outputs 0 0 1

输出0 0 1