as3 ByteArray转为十六进制(二进制十六进制表示) [英] as3 ByteArray to Hex (binary hex representation)

查看:1199
本文介绍了as3 ByteArray转为十六进制(二进制十六进制表示)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有人可以帮助我在这里我已经挣扎了几天。

我正在通过AS3 UDP数据报套接字接收UDP数据包一个二进制十六进制表示的格式(我相信是RAW UDP数据)。

当我收到As3的UDP数据包时,它的ByteArray格式,我需要转换回原来的十六进制格式。



这是它应该看起来如何:

  EF BE AD DE 
22 5C 88 06
5E 00 00 00
7C 11 FB 44
00 00 00 00
00 00 00 $
00 00 00 00
00 00 00 00
02 02 01 05
91 EE FE F4
04 00 00 00
00 00 01 00
11 00 00 00

这是我在Flash中的输出(它不需要相同的间距和行打破了同样的结构,从看起来像它的删除零?我不知道为什么它这样做):

十六进制= efbeaddea05b9515e0007d11fb440000000000000000221595ee76f540 00001011000



这里是我的as3函数:
$ b $ pre $ public function hex数据:ByteArray){
var hex:String =;
data.position = 0;
var len:uint = data.length;
for(var i:uint = 0; i< len; ++ i){
var byte:uint = data.readUnsignedByte();
十六进制+ = byte.toString(16).substr(-2);
}
trace(hex =+ hex);



$ b $ p
$ b $ p
$ b $

解决方案

它看起来像我找到了解决方案!因为我做了一些挖掘,碰到这个功能从飓风工具类Hex.as这似乎是在做伎俩!将在进一步调查后报告。

链接到课程: https://code.google.com/p/as3crypto/source/browse/trunk/as3crypto/src/ com / hurlant / util / Hex.as?r = 4



所有我需要改变的是在导入类之后:


$ b

  trace(hex =+ Hex.fromArray(event.data); 


$ b

下面是实际的函数:

pre $ public static function fromArray(array :ByteArray,colons:Boolean = false):String {
var s:String =;
for(var i:uint = 0; i< array.length; i ++){
substr(-2,2);
if(colons){
if(i< array.length-1)s + =:;
}
}
return s;
}

原始UDP数据包t(删除所有填充项):

  EFBEADDE275A89005E000000FA1DFB440000000000000000000000000000000002020105891B0E3A040000000000010011000000 



as3从byteArray转换为十六进制后收到UDP数据包:

 EFBEADDE655AF9025E000000FA1DFB4400000000000000000000000000000000020201058B1B4A3A040000000000010011000000 

(这些并不是完全相同的数据包,所以它们看起来有点不同)。

i hope someone can help me out here i have been struggling with this for a few days now.

i am receiving a UDP packet via AS3 UDP datagram socket in the format of a binary hex representation (which i believe to be RAW UDP data).

when i receive the UDP packet in as3 its in the ByteArray format which i need to convert back to the original Hexadecimal formatting.

this is how it should look:

EF BE AD DE
22 5C 88 06
5E 00 00 00
7C 11 FB 44
00 00 00 00
00 00 00 00
00 00 00 00
00 00 00 00
02 02 01 05
91 EE FE F4
04 00 00 00
00 00 01 00
11 00 00 00

this is my output in flash (it doesn't need the same spacing and line breaks just the same structure, from looking at it looks like its removing the zeros? i have no idea why its doing this):

hex= efbeaddea05b9515e0007d11fb440000000000000000221595ee76f54000001011000

here is my as3 function:

public function hex(data:ByteArray){
    var hex:String = "";
    data.position = 0;
    var len:uint = data.length;
    for (var i:uint = 0; i < len; ++i){
        var byte:uint = data.readUnsignedByte();
        hex += byte.toString(16).substr(-2);
    }
    trace("hex= "+hex);
}

any help would be greatly appreciated!!

解决方案

it looks like i have found a solution! as i did bit of digging and came across this function from the hurlant utilities class Hex.as this seems to be doing the trick! will report back after further investigation..

link to class: https://code.google.com/p/as3crypto/source/browse/trunk/as3crypto/src/com/hurlant/util/Hex.as?r=4

all i needed to change above was this, after importing the class:

trace("hex= "+Hex.fromArray(event.data);

here's the actual function:

public static function fromArray(array:ByteArray, colons:Boolean=false):String {
        var s:String = "";
        for (var i:uint=0;i<array.length;i++) {
                s+=("0"+array[i].toString(16)).substr(-2,2);
                if (colons) {
                        if (i<array.length-1) s+=":";
                }
        }
        return s;
}

original UDP packet (with all the padding removed):

EFBEADDE275A89005E000000FA1DFB440000000000000000000000000000000002020105891B0E3A040000000000010011000000

as3 received UDP packet after conversion from byteArray to Hexadecimal:

EFBEADDE655AF9025E000000FA1DFB4400000000000000000000000000000000020201058B1B4A3A040000000000010011000000

(these are not the exact same packets captured at the exact time, so they will look bit different).

这篇关于as3 ByteArray转为十六进制(二进制十六进制表示)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆