将整数转换为字节数组(Java)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1936857/
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
Convert integer into byte array (Java)
提问by Buttercup
what's a fast way to convert an Integer
into a Byte Array
?
将 an 转换Integer
为 a的快速方法是Byte Array
什么?
e.g. 0xAABBCCDD => {AA, BB, CC, DD}
例如 0xAABBCCDD => {AA, BB, CC, DD}
采纳答案by Gregory Pakosz
Have a look at the ByteBufferclass.
看看ByteBuffer类。
ByteBuffer b = ByteBuffer.allocate(4);
//b.order(ByteOrder.BIG_ENDIAN); // optional, the initial order of a byte buffer is always BIG_ENDIAN.
b.putInt(0xAABBCCDD);
byte[] result = b.array();
Setting the byte order ensures that result[0] == 0xAA
, result[1] == 0xBB
, result[2] == 0xCC
and result[3] == 0xDD
.
设置字节顺序保证了result[0] == 0xAA
,result[1] == 0xBB
,result[2] == 0xCC
和result[3] == 0xDD
。
Or alternatively, you could do it manually:
或者,您可以手动执行此操作:
byte[] toBytes(int i)
{
byte[] result = new byte[4];
result[0] = (byte) (i >> 24);
result[1] = (byte) (i >> 16);
result[2] = (byte) (i >> 8);
result[3] = (byte) (i /*>> 0*/);
return result;
}
The ByteBuffer
class was designed for such dirty hands tasks though. In fact the private java.nio.Bits
defines these helper methods that are used by ByteBuffer.putInt()
:
不过,该ByteBuffer
课程是为此类脏手任务而设计的。事实上,私有java.nio.Bits
定义了这些被使用的辅助方法ByteBuffer.putInt()
:
private static byte int3(int x) { return (byte)(x >> 24); }
private static byte int2(int x) { return (byte)(x >> 16); }
private static byte int1(int x) { return (byte)(x >> 8); }
private static byte int0(int x) { return (byte)(x >> 0); }
回答by notnoop
You can use BigInteger
:
您可以使用BigInteger
:
From Integers:
从整数:
byte[] array = BigInteger.valueOf(0xAABBCCDD).toByteArray();
System.out.println(Arrays.toString(array))
// --> {-86, -69, -52, -35 }
The returned array is of the size that is needed to represent the number, so it could be of size 1, to represent 1 for example. However, the size cannot be more than four bytes if an int is passed.
返回的数组具有表示数字所需的大小,因此它的大小可能为 1,例如表示 1。但是,如果传递的是 int,则大小不能超过四个字节。
From Strings:
从字符串:
BigInteger v = new BigInteger("AABBCCDD", 16);
byte[] array = v.toByteArray();
However, you will need to watch out, if the first byte is higher 0x7F
(as is in this case), where BigInteger would insert a 0x00 byte to the beginning of the array. This is needed to distinguish between positive and negative values.
但是,您需要注意,如果第一个字节更高0x7F
(在这种情况下),BigInteger 会在数组的开头插入一个 0x00 字节。这是区分正值和负值所必需的。
回答by Pascal Thivent
Using BigInteger
:
使用BigInteger
:
private byte[] bigIntToByteArray( final int i ) {
BigInteger bigInt = BigInteger.valueOf(i);
return bigInt.toByteArray();
}
Using DataOutputStream
:
使用DataOutputStream
:
private byte[] intToByteArray ( final int i ) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
dos.writeInt(i);
dos.flush();
return bos.toByteArray();
}
Using ByteBuffer
:
使用ByteBuffer
:
public byte[] intToBytes( final int i ) {
ByteBuffer bb = ByteBuffer.allocate(4);
bb.putInt(i);
return bb.array();
}
回答by daz
use this function it works for me
使用这个功能对我有用
public byte[] toByteArray(int value) {
return new byte[] {
(byte)(value >> 24),
(byte)(value >> 16),
(byte)(value >> 8),
(byte)value};
}
it translates the int into a byte value
它将 int 转换为字节值
回答by Pang
If you like Guava, you may use its Ints
class:
For int
→ byte[]
, use toByteArray()
:
对于int
→ byte[]
,使用toByteArray()
:
byte[] byteArray = Ints.toByteArray(0xAABBCCDD);
Result is {0xAA, 0xBB, 0xCC, 0xDD}
.
结果是{0xAA, 0xBB, 0xCC, 0xDD}
。
Its reverse is fromByteArray()
or fromBytes()
:
它的反面是fromByteArray()
或fromBytes()
:
int intValue = Ints.fromByteArray(new byte[]{(byte) 0xAA, (byte) 0xBB, (byte) 0xCC, (byte) 0xDD});
int intValue = Ints.fromBytes((byte) 0xAA, (byte) 0xBB, (byte) 0xCC, (byte) 0xDD);
Result is 0xAABBCCDD
.
结果是0xAABBCCDD
。
回答by user3424779
Here's a method that should do the job just right.
这是一种应该正确完成工作的方法。
public byte[] toByteArray(int value)
{
final byte[] destination = new byte[Integer.BYTES];
for(int index = Integer.BYTES - 1; index >= 0; index--)
{
destination[i] = (byte) value;
value = value >> 8;
};
return destination;
};
回答by Muskovets
It's my solution:
这是我的解决方案:
public void getBytes(int val) {
byte[] bytes = new byte[Integer.BYTES];
for (int i = 0;i < bytes.length; i ++) {
int j = val % Byte.MAX_VALUE;
bytes[i] = (j == 0 ? Byte.MAX_VALUE : j);
}
}
Also String
y method:
还有String
y方法:
public void getBytes(int val) {
String hex = Integer.toHexString(val);
byte[] val = new byte[hex.length()/2]; // because byte is 2 hex chars
for (int i = 0; i < hex.length(); i+=2)
val[i] = Byte.parseByte("0x" + hex.substring(i, i+2), 16);
return val;
}
回答by wai
This will help you.
这会帮助你。
import java.nio.ByteBuffer;
import java.util.Arrays;
public class MyClass
{
public static void main(String args[]) {
byte [] hbhbytes = ByteBuffer.allocate(4).putInt(16666666).array();
System.out.println(Arrays.toString(hbhbytes));
}
}
回答by Matt
Can also shift -
还可以移——
byte[] ba = new byte[4];
int val = Integer.MAX_VALUE;
for(byte i=0;i<4;i++)
ba[i] = (byte)(val >> i*8);
//ba[3-i] = (byte)(val >> i*8); //Big-endian
回答by helmy
Simple solution which properly handles ByteOrder:
正确处理 ByteOrder 的简单解决方案:
ByteBuffer.allocate(4).order(ByteOrder.nativeOrder()).putInt(yourInt).array();
ByteBuffer.allocate(4).order(ByteOrder.nativeOrder()).putInt(yourInt).array();