Hi here the method I use to convert an int to ByteArray. I assume that
it suit with your needs.
Attention int is 4 Bytes long, if you want to produce longer byteArrays
with leading zeros, ajust the length variable.
private byte[] makeByteArray(int value, int length) {
byte[] b = new byte[length];
for (int i = 0; i < length; i++) {
int offset = (b.length - 1 - i) * 8;
b
= (byte) ((value >>> offset) & 0xFF);
}
if(b.length > 4) {
for(int i = 0; i < b.length - 4; i++) {
b = 0;
}
}
return b;
}
cryptogirl said:
Hello,
I need to convert an int array to a byte array. The int array is
divided into elements of 8 bits long. Any code would help.
Thank you
List<Byte> bytes = new ArrayList<Byte>();
int[] array = ...
for (int i = 0; i < array.length; i++) {
bytes.add((byte)(array
>> 24));
bytes.add((byte)(array >> 16));
bytes.add((byte)(array >> 8));
bytes.add((byte)(array >> 0));
}
byte[] barry = bytes.asArray(new byte[bytes.size()]);
there's probably a quicker way