ByteArray 字节操作

ByteArray 类

CBrother提供了ByteArray类来处理字节流

  1. var myByte = new ByteArray(1024 * 10); //创建一个10KB的缓冲
  2. var myByte2 = new ByteArray(); //不传值默认为4KB大小
函数描述参数返回值用法
setLittleEndian(little) 设置低位编址(LE) little:true为低位编址 不设置默认是高位编址(BE) myByte.setLittleEndian(true)
isLittleEndian() 查看编址方式 true为低位编址(LE) false为高位编址(BE) var isLE = myByte.isLittleEndian()
getSize() 获取缓冲长度 长度,构造时候传入的值 var size = myByte.getSize()
expand(newLen) 扩展缓冲区,数据保留 newLen:字节 myByte.expand(1024 * 100)
setWritePos(pos) 设置写入起始位置 写入后坐标会自动加 0 <= pos < getSize() myByte.setWritePos(0)
getWritePos() 获取当前写坐标 坐标值 var pos = myByte.getWritePos()
setReadPos(pos) 设置读取起始位置 读取后坐标会自动加 0 <= pos < getSize() myByte.setReadPos(0)
getReadPos() 获取当前读坐标 坐标值 var pos = myByte.getReadPos()
writeInt(v) 写入一个整数(4字节) v:写入值 true:写入成功 myByte.writeInt(1)
writeFloat(v) 写入一个浮点数(4字节) v:写入值 true:写入成功 myByte.writeFloat(1.0)
writeBool(v) 写入布尔值(1字节) v:写入值 true:写入成功 myByte.writeBool(true)
writeByte(v) 写入一个字符(1字节) v:写入值 true:写入成功 myByte.writeByte('c')
writeString(v) 写入一个字符串 v:写入值 true:写入成功 myByte.writeString("i'm a string")
writeBytes(v,len) 写入多个字节 v:ByteArray对象 len:长度,不传写到结束 从v的ReadPos写len个字节 true:写入成功 myByte.writeBytes(v,100)
readInt() 读取整数(4字节) 整数 var v = myByte.readInt()
readFloat() 读取浮点数(4字节) 浮点数 var v = myByte.readFloat()
readBool() 读取布尔值(1字节) 布尔值 var v = myByte.readBool()
readByte() 读取一个字符(1字节) ascii码 var v = myByte.readByte()
readString() 读取一个字符串 字符串 var v = myByte.readString()
readString(len) 读取指定长度的一个字符串 字符串 var v = myByte.readString(10)
readBytes(len) 读取多个字节 len:读取的长度 ByteArray对象 var other = myByte.readBytes(100)
getByte(pos) 读取pos对应的字节 不影响读坐标 0 <= pos < getSize() ascii码 var v = myByte.getByte(0)
setByte(pos,v) 修改pos对应的字节 不影响写坐标 0 <= pos < getSize() v:ascii码 true:写入成功 myByte.setByte(0,'a')
clear() 清空,读写坐标归0 myByte.clear()
copy(other,selfBegin,otherBegin,len) 把other从otherBegin的len个字节复制给自己selfBegin other:ByteArray selfBegin:int otherBegin:int len:int true复制成功 myByte.copy(other,10,20,50)

·高位编址(BE)与低位编址(LE)

Little-endian:将低序字节存储在起始地址

Big-endian:将高序字节存储在起始地址

  1. function main(parm)
  2. {
  3. var myByte = new ByteArray();
  4. myByte.writeInt(1);
  5. for(var i = 0 ; i < 4 ; i++)
  6. {
  7. print "BigEndian:" + i + " " + myByte.readByte();
  8. }
  9. var myByte = new ByteArray();
  10. myByte.setLittleEndian(true);
  11. myByte.writeInt(1);
  12. for(var i = 0 ; i < 4 ; i++)
  13. {
  14. print "Little:" + i + " " + myByte.readByte();
  15. }
  16. }

结果:

  1. BigEndian:0 0
  2. BigEndian:1 0
  3. BigEndian:2 0
  4. BigEndian:3 1
  5. Little:0 1
  6. Little:1 0
  7. Little:2 0
  8. Little:3 0

·ByteArray例子

  1. function main(parm)
  2. {
  3. var myByte = new ByteArray(1024 * 10);
  4. myByte.writeInt(1);
  5. myByte.writeString("aaaa");
  6. myByte.setReadPos(0);
  7. print myByte.readInt();
  8. print myByte.readString();
  9. for(var i = 0 ; i < 4 ; i++)
  10. {
  11. var b = myByte.getByte(i) ^ 0xff;
  12. myByte.setByte(i,b);
  13. }
  14. myByte.setReadPos(0);
  15. print "xor int:" + myByte.readInt();
  16. }
  1. 1
  2. aaaa
  3. xor int:-2