使用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 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值