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

发布于 2024-07-07 04:13:33 字数 288 浏览 9 评论 0原文

我正在尝试将 int 转换为代表该 int (大端)的三个 字节

我确信它与按位和移位有关。 但我不知道该怎么做。

例如:

int myInt;

// some code

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

*注意,我知道 int 是 4 个字节,这三个字节有可能上溢/下溢。

I am trying to convert an int into three bytes representing that 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

情场扛把子 2024-07-14 04:13:33

要获取最低有效字节:

b3 = myInt & 0xFF;

第二个最低有效字节:

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

和第三个最低有效字节:

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

说明:

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

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.

二智少女 2024-07-14 04:13:33
byte b1 = (myint >> 16) & 0xff;
byte b2 = (myint >> 8) & 0xff;
byte b3 = myint & 0xff;

我不确定这个在java中是如何实现的,我不是java开发者

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

复古式 2024-07-14 04:13:33

int 不能容纳 3 个字节。 但是,假设您知道这些特定的内容:

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

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

   byte b1 = (myInt & 0xff);
   myInt >>= 8;
   byte b2 = (myInt & 0xff);
   myInt >>= 8;
   byte b3 = (myInt & 0xff);
高跟鞋的旋律 2024-07-14 04:13:33

在 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);

输出 0 0 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

十年九夏 2024-07-14 04:13:33

如果是正整数值,Jeremy 的答案是正确的。 如果转换对于负值应该是正确的,由于二进制补码格式,它会稍微复杂一些( https://en.wikipedia.org/wiki/Two%27s_complement)。 诀窍是消除感兴趣的位(较低有效位)和“符号”位之间的间隙。 一种简单的方法是数字相乘。

    int myIntMultiplied = myInt * 256;

    byte b1, b2, b3;

    b3 = (byte) ((myIntMultiplied >> 8) & 0xFF);
    b2 = (byte) ((myIntMultiplied >> 16) & 0xFF);
    b1 = (byte) ((myIntMultiplied >> 24) & 0xFF);

这将以正确的负值补码格式结束。

PS:您可以这样检查和比较二进制表示:

Integer.toBinaryString(myInt);
Integer.toBinaryString(myIntMultiplied );

The answer of Jeremy is correct in case of positive integer value. If the conversion should be correct for negative values, it is little more complicated due to two's-complement format (https://en.wikipedia.org/wiki/Two%27s_complement). The trick is to remove the gap between the interesting bits (the less significant bits) and the 'sign' bit. One easy method is multiplication of the number.

    int myIntMultiplied = myInt * 256;

    byte b1, b2, b3;

    b3 = (byte) ((myIntMultiplied >> 8) & 0xFF);
    b2 = (byte) ((myIntMultiplied >> 16) & 0xFF);
    b1 = (byte) ((myIntMultiplied >> 24) & 0xFF);

This will end up with correct two's-complement format for negative values.

PS: You can check and compare binary representation this way:

Integer.toBinaryString(myInt);
Integer.toBinaryString(myIntMultiplied );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文