Java 如何将整数拆分为 2 字节二进制文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1735840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I split an integer into 2 byte binary?
提问by Kevin Boyd
Given
给定的
private int width = 400;
private byte [] data = new byte [2];
I want to split the integer "width" into two bytes and load data[0] with the high byte and data[1] with the low byte.
我想将整数“宽度”分成两个字节,并用高字节加载 data[0],用低字节加载 data[1]。
That is binary value of 400 = 1 1001 0000 so data[0] should contain 0000 0001 and data[1] should contain 1001 0000
那是 400 = 1 1001 0000 的二进制值所以 data[0] 应该包含 0000 0001 并且 data[1] 应该包含 1001 0000
采纳答案by Stephan202
Using simple bitwise operations:
使用简单的按位运算:
data[0] = (byte) (width & 0xFF);
data[1] = (byte) ((width >> 8) & 0xFF);
How it works:
这个怎么运作:
& 0xFF
masks all but the lowest eight bits.>> 8
discards the lowest 8 bits by moving all bits 8 places to the right.- The cast to byte is necessary because these bitwise operations work on an
int
and return anint
, which is a bigger data type thanbyte
. The case is safe, since all non-zero bits will fit in thebyte
. For more information, see Conversions and Promotions.
& 0xFF
屏蔽除最低八位之外的所有位。>> 8
通过将所有位向右移动 8 位来丢弃最低 8 位。- 强制转换为字节是必要的,因为这些按位运算适用于 an
int
并返回 anint
,这是比byte
. 这种情况是安全的,因为所有非零位都适合byte
. 有关更多信息,请参阅转化和促销。
Edit:Taylor Lcorrectly remarks that though >>
works in this case, it may yield incorrect results if you generalize this code to fourbytes (since in Java an int
is 32 bits). In that case, it's better to use >>>
instead of >>
. For more information, see the Java tutorial on Bitwise and Bit Shift Operators.
编辑:Taylor L正确地指出,虽然>>
在这种情况下有效,但如果将此代码概括为四个字节(因为在 Java 中int
是 32 位),它可能会产生不正确的结果。在这种情况下,最好使用>>>
代替>>
。有关详细信息,请参阅有关按位和位移运算符的Java 教程。
回答by Dmitry
Integer is 32 bits (=4 bytes) in java, you know?
Java 中的整数是 32 位(=4 字节),你知道吗?
width & 0xff will give you the first byte, width & 0xff00 >> 8 will give you the second, etc.
width & 0xff 会给你第一个字节, width & 0xff00 >> 8 会给你第二个,依此类推。
回答by Rod Hyde
To get the high byte, shift right by 8 bits then mask off the top bytes. Similarly, to get the low byte just mask off the top bytes.
要获得高字节,请右移 8 位然后屏蔽高字节。同样,要获得低字节,只需屏蔽高字节。
data[0] = (width >> 8) & 0xff;
data[1] = width & 0xff;
回答by alltom
int width = 400;
byte [] data = new byte [2];
data[0] = (byte) ((width & 0xFF00) >> 8);
data[1] = (byte) (width & 0xFF);
for(int b = 0; b < 2; b++) {
System.out.println("printing byte " + b);
for(int i = 7; i >= 0; i--) {
System.out.println(data[b] & 1);
data[b] = (byte) (data[b] >> 1);
}
}
回答by Taylor Leese
This should do what you want for a 4 byte int. Note, it stores the low byte at offset 0. I'll leave it as an exercise to the reader to order them as needed.
这应该为 4 字节 int 做你想要的。请注意,它将低字节存储在偏移量 0 处。我将把它留给读者作为练习,以便根据需要对它们进行排序。
public static byte[] intToBytes(int x) {
byte[] bytes = new byte[4];
for (int i = 0; x != 0; i++, x >>>= 8) {
bytes[i] = (byte) (x & 0xFF);
}
return bytes;
}
回答by starblue
For converting two bytes the cleanest solution is
为了转换两个字节,最干净的解决方案是
data[0] = (byte) width;
data[1] = (byte) (width >>> 8);
For converting an integer to four bytes the code would be
要将整数转换为四个字节,代码将是
data[0] = (byte) width;
data[1] = (byte) (width >>> 8);
data[2] = (byte) (width >>> 16);
data[3] = (byte) (width >>> 24);
It doesn't matter whether >> or >>> is used for shifting, any one bits created by sign extension will not end up in the resulting bytes.
无论是 >> 还是 >>> 用于移位,符号扩展创建的任何一位都不会在结果字节中结束。
See also this answer.
另请参阅此答案。
回答by Peter Lawrey
I suggest you have a look at the source for HeapByteBuffer. It has the conversion code for all primitive data types. (In fact you could just use a ByteBuffer ;)
我建议您查看 HeapByteBuffer 的源代码。它具有所有原始数据类型的转换代码。(事实上你可以只使用一个 ByteBuffer ;)