Flash中是没有Byte类型的。ByteArray却可以读取字节流。存储在ByteArray中的自然是Byte,那么,其Write/Read Byte的方法是怎么回事?
Write:The low 8 bits of the parameter are used. The high 24 bits are ignored."
是的,Int的低8位被写入,剩余24位被忽略。ReadByte返回的是一个int,The returned value is in the range -128 to 127,即{-27, 27-1}。这里的定义与Java中Byte类型定义相同,是8位有符号整数。
然而事实情况很玄妙,不通过readByte来读取,而是直接通过index读取各个位的值,获取的数值是0到255的8位无符号整数(补码存储,买个关子,想知道原因的同学请Google之)。如果有人要写一些底层数据或者是移植工作,切记!如有谬误,请指正。
相关代码:
Write:The low 8 bits of the parameter are used. The high 24 bits are ignored."
是的,Int的低8位被写入,剩余24位被忽略。ReadByte返回的是一个int,The returned value is in the range -128 to 127,即{-27, 27-1}。这里的定义与Java中Byte类型定义相同,是8位有符号整数。
然而事实情况很玄妙,不通过readByte来读取,而是直接通过index读取各个位的值,获取的数值是0到255的8位无符号整数(补码存储,买个关子,想知道原因的同学请Google之)。如果有人要写一些底层数据或者是移植工作,切记!如有谬误,请指正。
相关代码:
var byte:ByteArray = new ByteArray();
byte.writeInt(255); //byte.writeInt(256); 127, 128, -1, -128,etc
var arr:Array = [];
for(var i:int = 0; i < byte.length; i++)
{
arr[i] = byte[i];
}
trace(arr);
byte.writeInt(255); //byte.writeInt(256); 127, 128, -1, -128,etc
var arr:Array = [];
for(var i:int = 0; i < byte.length; i++)
{
arr[i] = byte[i];
}
trace(arr);



而通过索引来读取是直接按照二进制存储的方式输出的无符号数。
谢谢Qizhi的提醒
关于负数的物理存储方式我也是看到这篇文章后才知道的
http://www.alchemy3d.cn/blog/post/17.html