ByteArray、16进制、字符串之间的转换

ByteArray、16进制、字符串之间的转换:

package fengzi.convert
{
import flash.utils.ByteArray;
public class ByteArrayTranslated
{ /***
* 通过hax数据返回ByteArray
* @param hax 格式 "AA5A000100FF"
***/
public static function getHax(hax:String):ByteArray
{
var byte:ByteArray=new ByteArray();
for (var i:uint = 0; i<hax.length; i=i+2)
{
trace(hax.substr(i, 2));
trace(uint("0x"+hax.substr(i, 2)));
byte.writeByte(uint("0x"+hax.substr(i, 2)));
} return byte;
} /***
* 16进制数据返回ByteArray
* @param str 格式 "AA5A000100FF"
* @return bytes
***/
public static function from16ToByteArray(str:String):ByteArray
{
var bytes:ByteArray=new ByteArray();
var b_arr:Array = str.split("");
var b_length:int = b_arr.length;
for (var i:int=0; i<b_length-1; i+=2)
{
var b_char1:String = b_arr[i];
var b_char2:String = b_arr[i + 1];
var b_byte:int=parseInt(b_char1+b_char2,16);
//trace("b_byte:",b_byte);
bytes.writeByte(b_byte);
}
return bytes;
} /***
* String转ByteArray
* @param str 字符
* @return bytes
***/
public static function convertStringToByteArray(str:String):ByteArray
{
var bytes:ByteArray;
if (str)
{
bytes=new ByteArray();
bytes.writeUTFBytes(str);
}
return bytes;
} /***
* ByteArray转String
* @param bytes
* @return str
***/
public static function convertByteArrayToString(bytes:ByteArray):String
{
var str:String;
if (bytes)
{
bytes.position = 0;
str = bytes.readUTFBytes(bytes.length);
}
return str;
} /***
* 把ByteArray转换为16进制的形式的字符串
* @param ba
* @param name
* @return
***/
public static function byteArrayTo16(ba:ByteArray):String
{
ba.position = 0;
var b_str:String = "";
while (ba.bytesAvailable > 0)
{
var b_s:String = ba.readUnsignedByte().toString(16);
// trace("b_s:",b_s);
if (b_s.length < 2)
{
b_s = "0" + b_s;
}
b_str += b_s;
}
return b_str;
} } }

  

上一篇:lower_bound &&upper_bound


下一篇:Flutter之内置动画(转)