如何快速将 Integer
转换为 Byte Array
?
例如:0xAABBCCDD => {AA, BB, CC, DD}
请查看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();
设置字节序可以确保 result[0] == 0xAA
,result[1] == 0xBB
,result[2] == 0xCC
和 result[3] == 0xDD
。
或者,您也可以手动完成此操作:
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;
}
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); }
使用BigInteger
:
private byte[] bigIntToByteArray( final int i ) {
BigInteger bigInt = BigInteger.valueOf(i);
return bigInt.toByteArray();
}
使用 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();
}
使用 ByteBuffer
:
public byte[] intToBytes( final int i ) {
ByteBuffer bb = ByteBuffer.allocate(4);
bb.putInt(i);
return bb.array();
}
我用过这个函数,它对我有效
public byte[] toByteArray(int value) {
return new byte[] {
(byte)(value >> 24),
(byte)(value >> 16),
(byte)(value >> 8),
(byte)value};
}
它将整数转换为字节值
将int
转换为byte[]
,请使用toByteArray()
方法:
byte[] byteArray = Ints.toByteArray(0xAABBCCDD);
结果是{0xAA, 0xBB, 0xCC, 0xDD}
。
它的反转是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);
结果为 0xAABBCCDD
。
BigInteger
:byte[] array = BigInteger.valueOf(0xAABBCCDD).toByteArray();
System.out.println(Arrays.toString(array))
// --> {-86, -69, -52, -35 }
BigInteger v = new BigInteger("AABBCCDD", 16);
byte[] array = v.toByteArray();
然而,需要注意的是,如果第一个字节大于0x7F
(在这种情况下),BigInteger会在数组开头插入一个0x00字节。这是为了区分正数和负数。
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
以下是处理ByteOrder的简单解决方案:
ByteBuffer.allocate(4).order(ByteOrder.nativeOrder()).putInt(yourInt).array();
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;
};
这会对你有所帮助。
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));
}
}
在Android上非常简单
int i=10000;
byte b1=(byte)Color.alpha(i);
byte b2=(byte)Color.red(i);
byte b3=(byte)Color.green(i);
byte b4=(byte)Color.blue(i);