Java整数到字节数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2183240/
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
Java integer to byte array
提问by stefan
I got an integer: 1695609641
我得到一个整数: 1695609641
when I use method:
当我使用方法时:
String hex = Integer.toHexString(1695609641);
system.out.println(hex);
gives:
给出:
6510f329
but I want a byte array:
但我想要一个字节数组:
byte[] bytearray = new byte[] { (byte) 0x65, (byte)0x10, (byte)0xf3, (byte)0x29};
How can I make this?
我怎样才能做到这一点?
采纳答案by dfa
using Java NIO's ByteBufferis very simple:
使用 Java NIO 的ByteBuffer非常简单:
byte[] bytes = ByteBuffer.allocate(4).putInt(1695609641).array();
for (byte b : bytes) {
System.out.format("0x%x ", b);
}
output:
输出:
0x65 0x10 0xf3 0x29
回答by Brian Agnew
integer & 0xFF
for the first byte
对于第一个字节
(integer >> 8) & 0xFF
for the second and loop etc., writing into a preallocated byte array. A bit messy, unfortunately.
对于第二个和循环等,写入预先分配的字节数组。有点乱,可惜。
回答by Grzegorz Oledzki
How about:
怎么样:
public static final byte[] intToByteArray(int value) {
return new byte[] {
(byte)(value >>> 24),
(byte)(value >>> 16),
(byte)(value >>> 8),
(byte)value};
}
The idea is not mine. I've taken it from some post on dzone.com.
这个主意不是我的。我从dzone.com 上的一些帖子中获取了它。
回答by Carl Smotricz
byte[] conv = new byte[4];
conv[3] = (byte) input & 0xff;
input >>= 8;
conv[2] = (byte) input & 0xff;
input >>= 8;
conv[1] = (byte) input & 0xff;
input >>= 8;
conv[0] = (byte) input;
回答by vmpn
BigInteger.valueOf(1695609641).toByteArray()
BigInteger.valueOf(1695609641).toByteArray()
回答by mlohbihler
The class org.apache.hadoop.hbase.util.Bytes has a bunch of handy byte[] conversion methods, but you might not want to add the whole HBase jar to your project just for this purpose. It's surprising that not only are such method missing AFAIK from the JDK, but also from obvious libs like commons io.
org.apache.hadoop.hbase.util.Bytes 类有一堆方便的 byte[] 转换方法,但您可能不想仅仅为了这个目的将整个 HBase jar 添加到您的项目中。令人惊讶的是,这种方法不仅缺少 JDK 中的 AFAIK,而且还缺少像 commons io 这样明显的库。
回答by HaoQi Li
The chunks below work at least for sending an int over UDP.
下面的块至少可以用于通过 UDP 发送 int。
int to byte array:
int 到字节数组:
public byte[] intToBytes(int my_int) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out = new ObjectOutputStream(bos);
out.writeInt(my_int);
out.close();
byte[] int_bytes = bos.toByteArray();
bos.close();
return int_bytes;
}
byte array to int:
字节数组到int:
public int bytesToInt(byte[] int_bytes) throws IOException {
ByteArrayInputStream bis = new ByteArrayInputStream(int_bytes);
ObjectInputStream ois = new ObjectInputStream(bis);
int my_int = ois.readInt();
ois.close();
return my_int;
}
回答by lyon819
public static byte[] intToBytes(int x) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(bos);
out.writeInt(x);
out.close();
byte[] int_bytes = bos.toByteArray();
bos.close();
return int_bytes;
}
回答by superbob
My try :
我的尝试:
public static byte[] toBytes(final int intVal, final int... intArray) {
if (intArray == null || (intArray.length == 0)) {
return ByteBuffer.allocate(4).putInt(intVal).array();
} else {
final ByteBuffer bb = ByteBuffer.allocate(4 + (intArray.length * 4)).putInt(intVal);
for (final int val : intArray) {
bb.putInt(val);
}
return bb.array();
}
}
With it you can do this :
有了它,你可以这样做:
byte[] fourBytes = toBytes(0x01020304);
byte[] eightBytes = toBytes(0x01020304, 0x05060708);
Full class is here : https://gist.github.com/superbob/6548493, it supports initialization from shorts or long
完整课程在这里:https://gist.github.com/superbob/6548493,它支持从短裤或长期初始化
byte[] eightBytesAgain = toBytes(0x0102030405060708L);
回答by Yury Finchenko
byte[] IntToByteArray( int data ) {
byte[] result = new byte[4];
result[0] = (byte) ((data & 0xFF000000) >> 24);
result[1] = (byte) ((data & 0x00FF0000) >> 16);
result[2] = (byte) ((data & 0x0000FF00) >> 8);
result[3] = (byte) ((data & 0x000000FF) >> 0);
return result;
}