I would like to convert integers to bytes. I have an example in Actionscript and I need to convert it to Java. For the sake of simplicity let’s assume only one number, 1234. This is my Java code:
int[] a = {1234};
ByteBuffer byteBuffer = ByteBuffer.allocate(a.length * 4);
IntBuffer intBuffer = byteBuffer.asIntBuffer();
intBuffer.put(a);
byte[] array = byteBuffer.array();
for (int i=0; i < array.length; i++) {
Log.i(T, i + ": " + array[i]);
}
This gives me the following result:
0 : 0
1 : 0
2 : 4
3 : -46
While in Actionscript I have this:
var c:ByteArray = new ByteArray;
c.writeInt(1234);
for(var p:uint=0; p<c.length; p++) {
trace(p+" : "+c[p]);
}
And the result:
0 : 0
1 : 0
2 : 4
3 : 210
What am I doing wrong, why is the result different? Thanks!
Java uses unsigned bytes.
ActionScript probably signed by default.
This can print every byte as unsigned: