使用AS3输出ByteArray为16进制

 1 package  
 2 {
 3     import flash.utils.ByteArray;
 4     /**
 5      * 输出ByteArray为16进制
 6      * @author Rise
 7      */
 8     public class Byte2Hex 
 9     {
10         public static function Trace(bytes:ByteArray):void 
11         {
12             if (bytes == null) 
13             {
14                 trace("bytes is null");
15                 return;
16             }
17             var length:int = getHexLen(bytes.length);
18             length = length > 4 ? length : 4;
19             trace(getTitle(length));
20             bytes.position = 0;
21             for (var j:int = 0; bytes.bytesAvailable > 0; j += 16) 
22             {
23                 var line:String = fillHexLen(j, length) + " ";
24                 var str:String = "";
25                 for (var i:int = 0; i < 16; i++) 
26                 {
27                     if (bytes.bytesAvailable > 0) 
28                     {
29                         var char:int = bytes.readByte() & 0xFF;
30                         line += fillHexLen(char, 2) + " ";
31                         str += String.fromCharCode(char);
32                     }
33                     else
34                     {
35                         line += ".. ";
36                     }
37                 }
38                 trace(line, "\t", str);
39             }
40         }
41         
42         private static function fillHexLen(num:int, length:int):String 
43         {
44             var str:String = num.toString(16);
45             var zeros:String = "";
46             for (var i:int = 0; i < length - str.length; i++) 
47             {
48                 zeros += "0";
49             }
50             
51             return zeros + str;
52         }
53         
54         private static function getHexLen(length:int):int
55         {
56             var bit:int = 0x0F;
57             for (var i:int = 1; i <= 8; i++) 
58             {
59                 bit = bit << i | bit;
60                 if (bit > length) 
61                 {
62                     return i;
63                 }
64             }
65             return 8;
66         }
67         
68         private static function getTitle(length:int):String 
69         {
70             var title:String = "";
71             for (var i:int = 0; i < length; i++) 
72             {
73                 title += " ";
74             }
75             return(title + " 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15");
76         }
77     }
78 }

代码很烂,高手勿喷。

使用方法:

var bytes:ByteArray = new ByteArray;
bytes.endian = Endian.LITTLE_ENDIAN;
bytes.writeMultiByte("ABCDEFGLKAJSFKOIJOIJWELKJLJOI114asdfasdfasdfasdfasdfasdfaf", "utf-8");
bytes.writeInt(100000);
Byte2Hex.Trace(bytes);

Output

     00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15
0000 41 42 43 44 45 46 47 4c 4b 41 4a 53 46 4b 4f 49       ABCDEFGLKAJSFKOI
0010 4a 4f 49 4a 57 45 4c 4b 4a 4c 4a 4f 49 31 31 34       JOIJWELKJLJOI114
0020 61 73 64 66 61 73 64 66 61 73 64 66 61 73 64 66       asdfasdfasdfasdf
0030 61 73 64 66 61 73 64 66 61 66 a0 86 01 00 .. ..       asdfasdfaf †

 

posted @ 2013-11-23 17:47  至尊王者  阅读(4265)  评论(0编辑  收藏