skip to main |skip to sidebar
显示标签为“ByteArray”的博文。显示所有博文
显示标签为“ByteArray”的博文。显示所有博文

2008年1月27日

[AS3]截取 ByteArray 中某一范围的元素

/**
* 截取原始 ByteArray 中某一范围的元素构成的新 ByteArray
* 返回的 ByteArray 包括 start 元素以及从其开始到 end 元素(但不包括该元素)的所有元素。
*
* @param bytes 原始 ByteArray
* @param result 返回指针,如果为 null,将会创建新的 ByteArray
* @param start 开始下标,负数表示从后面开始数。
* @param end 结束下标,负数表示从后面开始数。
* @return result
*/

public static function slice(
bytes:ByteArray, result:IDataOutput = null,
start:int = 0, end:int = -1):IDataOutput
{
if(!bytes) {
throw new ArgumentError("Param: must not be null.");
}
var totalLength:int = bytes.length;
if(!result) result = new ByteArray();
if(start<0) start = totalLength + start;
if(end<0) end = totalLength + end;
var length:int = end - start + 1;
result.writeBytes(bytes, start, length);
return result;
}

2008年1月7日

[AS3]把 ByteArray 转换为 16 进制

/**
* 把 ByteArray 转换为 16 进制。
* 格式为 "BB BB BB BB BB BB BB BB|BB BB...","BB" 表示一个字节。
*
* @param bytes ByteArray
* @return 把 ByteArray 转换为16进制后的字符串。
*/

public static function toHex(bytes:ByteArray):String {
var pos:int = bytes.position;
bytes.position = 0;
var result:String = "";
while(bytes.bytesAvailable >= 8) {
result += $toHexNum(bytes.readUnsignedByte()) + " "
+ $toHexNum(bytes.readUnsignedByte()) + " "
+ $toHexNum(bytes.readUnsignedByte()) + " "
+ $toHexNum(bytes.readUnsignedByte()) + " "
+ $toHexNum(bytes.readUnsignedByte()) + " "
+ $toHexNum(bytes.readUnsignedByte()) + " "
+ $toHexNum(bytes.readUnsignedByte()) + " "
+ $toHexNum(bytes.readUnsignedByte()) + "|";
}
while(bytes.bytesAvailable>1) {
result += $toHexNum(bytes.readUnsignedByte()) + " "
}
if(bytes.bytesAvailable) {
result += $toHexNum(bytes.readUnsignedByte());
}
bytes.position = pos;
return result;
}

/**
* @private
*
* 把 1 个字节转换为 16 进制的字符串。
*
* @param n 1 个字节。
* @return 16 进制的字符串。
*/
private static function $toHexNum(n:uint):String {
return n <= 0xF ? "0" + n.toString(16) : n.toString(16);
}

2008年1月4日

[AS3]判断两个 ByteArray 是否相等

/**
* 判断两个 ByteArray 是否相等。
*
* @param a 一个 ByteArray。
* @param b 另一个 ByteArray。
* @return 如果相等,返回 true;否则返回 false。
*/

public static function eqByteArray(a:ByteArray, b:ByteArray):Boolean {
if(a.length != b.length) {
return false;
}
var posA:int = a.position;
var posB:int = b.position;
var result:Boolean = true;
a.position = b.position = 0;
while(a.bytesAvailable >= 4) {
if(a.readUnsignedInt() != b.readUnsignedInt()) {
result = false;
break;
}
}
if(result && a.bytesAvailable != 0) {
var last:int = a.bytesAvailable;
result =
last == 1 ? a.readByte() == b.readByte() :
last == 2 ? a.readShort() == b.readShort() :
last == 3 ? a.readShort() == b.readShort()
&& a.readByte() == b.readByte() :
true;
}
a.position = posA;
b.position = posB;
return result;
}