gpt4 book ai didi

java - 将 int 转换为字节 - Java 和 Actionscript 中的不同结果

转载 作者:行者123 更新时间:2023-11-29 03:37:33 26 4
gpt4 key购买 nike

我想将整数转换为字节。我在 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com