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

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

问题描述

我希望有人可以帮助我,我已经为此苦苦挣扎了几天.

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

我正在通过 AS3 UDP 数据报套接字以二进制十六进制表示形式(我认为是 RAW UDP 数据)接收 UDP 数据包.

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).

当我以 ByteArray 格式接收 as3 UDP 数据包时,我需要将其转换回原始的十六进制格式.

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

它应该是这样的:

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

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

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):

十六进制= efbeaddea05b9515e0007d11fb440000000000000000221595ee76f54000001011000

hex= efbeaddea05b9515e0007d11fb440000000000000000221595ee76f54000001011000

这是我的 as3 函数:

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);
}

任何帮助将不胜感激!

推荐答案

看来我找到了解决方案!因为我做了一些挖掘,并从 hurrant 实用程序类 Hex 中发现了这个函数.因为这似乎在起作用!将在进一步调查后报告..

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..

课程链接: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);

实际功能如下:

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;
}

原始UDP数据包(去除所有填充):

original UDP packet (with all the padding removed):

EFBEADDE275A89005E000000FA1DFB440000000000000000000000000000000002020105891B0E3A040000000000010011000000

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

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 to Hex(二进制十六进制表示)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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