【发布时间】:2010-12-28 13:19:39
【问题描述】:
将Integer 转换为Byte Array 的快速方法是什么?
例如0xAABBCCDD => {AA, BB, CC, DD}
【问题讨论】:
-
生成的字节数组的格式是否重要?你会用它做什么?
将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); }
【讨论】:
rewind 它。否则,你不会写任何东西! (去过那里,做到了!)
使用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();
}
【讨论】:
ByteBuffer 的回答会更直观。
使用这个对我有用的功能
public byte[] toByteArray(int value) {
return new byte[] {
(byte)(value >> 24),
(byte)(value >> 16),
(byte)(value >> 8),
(byte)value};
}
它将 int 转换为字节值
【讨论】:
对于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 }
返回的数组的大小是表示数字所需的大小,因此它的大小可以是 1,例如表示 1。但是,如果传递的是 int,则大小不能超过四个字节。
来自字符串:
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();
【讨论】:
这会对你有所帮助。
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));
}
}
【讨论】:
这是一种应该可以正确完成工作的方法。
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;
};
【讨论】:
这是我的解决方案:
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);
}
}
还有Stringy 方法:
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;
}
【讨论】:
用安卓很容易
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);
【讨论】: