我得到一个整数:
1695609641
当我使用方法时:
String hex = Integer.toHexString(1695609641);
system.out.println(hex);
给出:
6510f329
但我想要一个字节数组:
byte[] bytearray = new byte[] { (byte) 0x65, (byte)0x10, (byte)0xf3, (byte)0x29};
我该怎么做这个?
使用Java NIO的ByteBuffer非常简单:
byte[] bytes = ByteBuffer.allocate(4).putInt(1695609641).array();
for (byte b : bytes) {
System.out.format("0x%x ", b);
}
输出:
0x65 0x10 0xf3 0x29
BigInteger.valueOf(1695609641).toByteArray()
public static byte[] convertToByteArray(final int[] pIntArray)
{
final byte[] array = new byte[pIntArray.length * 4];
for (int j = 0; j < pIntArray.length; j++)
{
final int c = pIntArray[j];
array[j * 4] = (byte)((c & 0xFF000000) >> 24);
array[j * 4 + 1] = (byte)((c & 0xFF0000) >> 16);
array[j * 4 + 2] = (byte)((c & 0xFF00) >> 8);
array[j * 4 + 3] = (byte)(c & 0xFF);
}
return array;
}
public static int[] convertToIntArray(final byte[] pByteArray)
{
final int[] array = new int[pByteArray.length / 4];
for (int i = 0; i < array.length; i++)
array[i] = (((int)(pByteArray[i * 4]) << 24) & 0xFF000000) |
(((int)(pByteArray[i * 4 + 1]) << 16) & 0xFF0000) |
(((int)(pByteArray[i * 4 + 2]) << 8) & 0xFF00) |
((int)(pByteArray[i * 4 + 3]) & 0xFF);
return array;
}
请注意,由于符号传播等原因,在转换回 int 时需要“& 0xFF ...”。
第一个字节
(integer >> 8) & 0xFF
对于第二个和循环等,写入预先分配的字节数组。不幸的是,有点乱。
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;
}
字节数组转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;
}
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();
}
}
有了它,你可以做到这一点:
byte[] fourBytes = toBytes(0x01020304);
byte[] eightBytes = toBytes(0x01020304, 0x05060708);
完整类在这里:https://gist.github.com/superbob/6548493
,它支持从shorts或long初始化
byte[] eightBytesAgain = toBytes(0x0102030405060708L);
byte[] intToBytes(int i) {
return new byte[]{
(byte) (i >>> 24),
(byte) (i >>> 16),
(byte) (i >>> 8),
(byte) i
};
}
int bytesToInt(byte[] b) {
//Cuz int encode by complement-on-two
//For a negative, signed left shift operation will Fill the upper part of the binary with 1.
//That‘s a question for us to combine the meaningful part.
//Here, we execute a AND 0xFF operation, to implicitly convert a byte to int, and fill the upper part of the binary with 0
//So ,we got a positive number now.
//The next step just execute OR operation to combine the four part as an integer.
return (b[0]) << 24 |
(b[1] & 0xFF) << 16 |
(b[2] & 0xFF) << 8 |
(b[3] & 0xFF);
}
String.valueOf(<your-int>).getBytes()
对于我的目的来说,它更简单(将 int 写入文件)。更不用说,与编码 32 位 int 相比,编码时您可以使用少于 4 个字节的小值 (ascii)。