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 †
  • 相关阅读:
    树莓派系统安装初始化
    CentOS7搭建配置SVN服务器
    搭建web定时任务管理平台
    Linux 内存清理
    使用kubeadm安装Kubernetes
    Web页面执行shell命令
    解决"libc.so.6: version `GLIBC_2.14' not found"问题
    crontab 任务带日期输出
    Linux 源码安装 Python3
    MongoDB 数据恢复与导出
  • 原文地址:https://www.cnblogs.com/flying_bat/p/3439246.html
Copyright © 2011-2022 走看看