Réponses:
Jetez un œil à la classe 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();
Réglage de l' ordre des assure d'octets result[0] == 0xAA, result[1] == 0xBB, result[2] == 0xCCet result[3] == 0xDD.
Ou bien, vous pouvez le faire manuellement:
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;
}
La ByteBufferclasse a été conçue pour ces tâches de mains sales. En fait, le private java.nio.Bitsdéfinit ces méthodes d'assistance qui sont utilisées par 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); }
Utilisation BigInteger:
private byte[] bigIntToByteArray( final int i ) {
BigInteger bigInt = BigInteger.valueOf(i);
return bigInt.toByteArray();
}
Utilisation 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();
}
Utilisation ByteBuffer:
public byte[] intToBytes( final int i ) {
ByteBuffer bb = ByteBuffer.allocate(4);
bb.putInt(i);
return bb.array();
}
ByteBufferest plus intuitive si vous avez affaire à un int supérieur à 2 ^ 31 - 1.
utilise cette fonction ça marche pour moi
public byte[] toByteArray(int value) {
return new byte[] {
(byte)(value >> 24),
(byte)(value >> 16),
(byte)(value >> 8),
(byte)value};
}
il traduit l'int en une valeur d'octet
Si vous aimez Guava , vous pouvez utiliser sa Intsclasse:
Pour int→ byte[], utilisez toByteArray():
byte[] byteArray = Ints.toByteArray(0xAABBCCDD);
Le résultat est {0xAA, 0xBB, 0xCC, 0xDD}.
Son inverse est fromByteArray()ou 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);
Le résultat est 0xAABBCCDD.
Vous pouvez utiliser BigInteger:
À partir des nombres entiers:
byte[] array = BigInteger.valueOf(0xAABBCCDD).toByteArray();
System.out.println(Arrays.toString(array))
// --> {-86, -69, -52, -35 }
Le tableau retourné a la taille nécessaire pour représenter le nombre, il peut donc être de taille 1, pour représenter 1 par exemple. Toutefois, la taille ne peut pas être supérieure à quatre octets si un int est passé.
À partir de chaînes:
BigInteger v = new BigInteger("AABBCCDD", 16);
byte[] array = v.toByteArray();
Cependant, vous devrez faire attention, si le premier octet est plus élevé 0x7F(comme c'est le cas dans ce cas), où BigInteger insère un octet 0x00 au début du tableau. Ceci est nécessaire pour faire la distinction entre les valeurs positives et négatives.
très facile avec 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);
Voici une méthode qui devrait faire le travail correctement.
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;
};
C'est ma solution:
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);
}
}
Aussi Stringy méthode:
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;
}