将int转换为字节-Java&中的结果不同动作脚本 [英] Convert int to bytes - different result in Java & Actionscript

查看:75
本文介绍了将int转换为字节-Java&中的结果不同动作脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将整数转换为字节.我在Actionscript中有一个示例,我需要将其转换为Java.为了简单起见,我们仅假设一个数字1234.这是我的Java代码:

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]);
}

这给了我以下结果:

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]);
}

结果:

0 : 0
1 : 0
2 : 4
3 : 210

我在做什么错,为什么结果不同?谢谢!

What am I doing wrong, why is the result different? Thanks!

推荐答案

Java使用 unsigned 个字节.

Java uses unsigned bytes.

ActionScript 已签名.

ActionScript probably signed by default.

3 : -46 //signed
3 : 210 //unsigned

这可以将每个字节打印为无符号:

This can print every byte as unsigned:

System.out.println((b < 0 ? 256 + b : b));

这篇关于将int转换为字节-Java&amp;中的结果不同动作脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆