zoukankan      html  css  js  c++  java
  • 使用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, "	", 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 †
  • 相关阅读:
    批量启动application pool
    sql server文件另存为的时候,选择文件编码和换行
    insert into 和 where not exists
    tcp slowstart (TCP 慢启动)
    如何在CentOS7上改变网络接口名
    Window系统命令行调用控制面板程序
    Using Let’s Encrypt for free SSL Certs with Netscaler
    端口相关知识学习笔记
    win7下KiWi Syslog服务器的安装与配置
    MPS添加管理设备实例NS的过程
  • 原文地址:https://www.cnblogs.com/flying_bat/p/3439246.html
Copyright © 2011-2022 走看看