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

标签 java actionscript integer byte

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

相关文章:

由 Actionscript 转义的 Java 解码字符串

javascript - JS/Flash - 拖动包含 img 的 anchor vs 拖动不带 anchor 的图像 - 如何获取 anchor 内的 img?

C++验证数字并停止无限循环

flash - 在Actionscript中播放MXML SoundEffect

c - unsigned long long 十六进制格式打印

c++ - 将 float 转换为整数等价物

java - Spring路由因无效路由而出现404错误

对象的 Javafx 线图

java - 不合逻辑的空指针异常

java - 无法从 START_OBJECT token 中反序列化 java.util.ArrayList 的实例