Flex接收51单片机发送过来的16进制数据转换为String

private 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 static function toHexNum(n:uint):String{
//return 0<0xF?" "+n.toString(16):n.toString(16);
return String.fromCharCode(n.toString());
}

你可能感兴趣的:(String)