• 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;
    		}
    
    
    
    
    
    
    	}
    
    }
    

      

  • 相关阅读:
    Git 常用命令速查表
    Android的Fragment中onActivityResult不被调用
    git基本操作
    Android混淆
    Shell
    小米手机图标应用图标不改变的问题
    linux常用基本命令
    android新建的项目界面上没有显示怎么办?
    bzoj5249: [2018多省省队联测]IIIDX(线段树)
    bzoj1951: [Sdoi2010]古代猪文(Lucas+CRT+欧拉定理)
  • 原文地址:https://www.cnblogs.com/fengziwu/p/10913373.html
Copyright © 2020-2023  润新知