查看 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();
}
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
这是一种可以正确完成工作的方法。
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;
};
正确处理 ByteOrder 的简单解决方案:
ByteBuffer.allocate(4).order(ByteOrder.nativeOrder()).putInt(yourInt).array();
用安卓很容易
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);
这将对您有所帮助。
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 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);
}
}
还有 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;
}
不定期副业成功案例分享
rewind
它。否则,你不会写任何东西! (去过也做过!)