我想将整数转换为字节。我在 Actionscript 中有一个示例,我需要将其转换为 Java。为了简单起见,我们假设只有一个数字 1234。这是我的 Java 代码:
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]);
}
这给了我以下结果:
0 : 0
1 : 0
2 : 4
3 : -46
在 Actionscript 中我有这个:
var c:ByteArray = new ByteArray;
c.writeInt(1234);
for(var p:uint=0; p<c.length; p++) {
trace(p+" : "+c[p]);
}
结果:
0 : 0
1 : 0
2 : 4
3 : 210
我做错了什么,为什么结果不同?谢谢!
最佳答案
Java 使用无符号 字节。
ActionScript 可能默认签名。
3 : -46 //signed
3 : 210 //unsigned
这可以将每个字节打印为无符号:
System.out.println((b < 0 ? 256 + b : b));
关于java - 将 int 转换为字节 - Java 和 Actionscript 中的不同结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14720587/