什么是"ByteArray.uncompress()"?在AS3中等效于C#? [英] What is "ByteArray.uncompress()" in AS3 equivalent to in C#?

查看:97
本文介绍了什么是"ByteArray.uncompress()"?在AS3中等效于C#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将AS3(ActionScript 3)函数转换为C#.

I am trying to convert a AS3 (ActionScript 3) function to C#.

此ActionScript函数包含一个名为ByteArray的类,据我所知,它基本上就是听起来像lmao的东西.我认为这有点类似于C#中的byte [].无论如何,我已经尽力使用MemoryStream将代码转换为C#,然后向其写入字节,然后返回UTF8字符串,如下面的代码所示.但是,我感觉我做ActionScript代码的方式似乎不正确,这就是我上面提到的问题所在.

This ActionScript function contains a class called ByteArray which from what I am aware of it's basically what it sounds like lmao. I think it's kind of similar of how byte[] would be in C#. Anyway, I have tried my best to convert the code to C# using MemoryStream and then writing bytes to it, and then returning UTF8 string as you can see in my code below. However, I feel as if my way of doing how the ActionScript code does isn't accurate and that is where my question above comes in.

将它们的负数写入"loc1"(ByteArray)和"loc1.uncompress()"中,这就是我要失败的地方,并且想知道是否有人可以帮助我转换此函数,以便完全准确吗?

With them negative numbers being written into "loc1" (The ByteArray) and "loc1.uncompress()", that's where I feel like I am failing and was wondering if someone could help me out in converting this function so it's fully accurate?

除了这个问题之外,我还想问一下我在C#代码中对负数所做的操作是否正确,就像ActionScript代码是如何做到的一样?意味着很多(:

On top of that question, I would also like to ask if what I was doing with the negative numbers was correct in my C# code just like how the ActionScript code was doing it? Would mean a lot (:

(很抱歉,如果不能完全理解,并且我所说的内容不太匹配)

(Sorry if not fully understandable and if what I say doesn't match up as much)

ActionScript代码:

ActionScript Code:

private function p() : String
      {
         var _loc1_:ByteArray = new ByteArray();
         _loc1_.writeByte(120);
         _loc1_.writeByte(-38);
         _loc1_.writeByte(99);
         _loc1_.writeByte(16);
         _loc1_.writeByte(12);
         _loc1_.writeByte(51);
         _loc1_.writeByte(41);
         _loc1_.writeByte(-118);
         _loc1_.writeByte(12);
         _loc1_.writeByte(50);
         _loc1_.writeByte(81);
         _loc1_.writeByte(73);
         _loc1_.writeByte(49);
         _loc1_.writeByte(-56);
         _loc1_.writeByte(13);
         _loc1_.writeByte(48);
         _loc1_.writeByte(54);
         _loc1_.writeByte(54);
         _loc1_.writeByte(14);
         _loc1_.writeByte(48);
         _loc1_.writeByte(46);
         _loc1_.writeByte(2);
         _loc1_.writeByte(0);
         _loc1_.writeByte(45);
         _loc1_.writeByte(-30);
         _loc1_.writeByte(4);
         _loc1_.writeByte(-16);
         _loc1_.uncompress();
         _loc1_.position = 0;
         return _loc1_.readUTF();
      }

我的C#代码:

public string p()
        {
            MemoryStream loc1 = new MemoryStream();
            loc1.WriteByte((byte)120);
            loc1.WriteByte((byte)~-38);
            loc1.WriteByte((byte)99);
            loc1.WriteByte((byte)16);
            loc1.WriteByte((byte)12);
            loc1.WriteByte((byte)51);
            loc1.WriteByte((byte)41);
            loc1.WriteByte((byte)~-118);
            loc1.WriteByte((byte)12);
            loc1.WriteByte((byte)50);
            loc1.WriteByte((byte)81);
            loc1.WriteByte((byte)73);
            loc1.WriteByte((byte)49);
            loc1.WriteByte((byte)~-56);
            loc1.WriteByte((byte)13);
            loc1.WriteByte((byte)48);
            loc1.WriteByte((byte)54);
            loc1.WriteByte((byte)54);
            loc1.WriteByte((byte)14);
            loc1.WriteByte((byte)48);
            loc1.WriteByte((byte)46);
            loc1.WriteByte((byte)2);
            loc1.WriteByte((byte)0);
            loc1.WriteByte((byte)45);
            loc1.WriteByte((byte)~-30);
            loc1.WriteByte((byte)4);
            loc1.WriteByte((byte)~-16);
            loc1.Position = 0;
            return Encoding.UTF8.GetString(loc1.ToArray());
        }

推荐答案

(1)
@Jimmy给了您一个很好的答案.
这就是他告诉您"用0xFF遮罩" 的意思,这样您的-38会被遮罩为:

(1)
@Jimmy has given you a good Answer.
This is what he meant when he told you "to mask with 0xFF" so that your -38 becomes masked as:

loc1.WriteByte( (byte)(-38 & 0xFF) );

对具有负sign的任何其他值执行相同的上述逻辑.

Do the same above logic for any other values that have a minus sign.

(2)
如果仅使用以十六进制而不是十进制编写的值,则可能会更容易.这意味着您应该写等效的十六进制0xFF而不是十进制255,因为字节应该以十六进制表示. WriteByte会自动转换您的小数位,但并不能帮助您了解正在发生的事情...

(2)
It might be easier if you just use values written in hex instead of decimal. This means instead of decimal 255 you write equivalent hex of 0xFF since bytes are supposed to be in hex. The WriteByte is auto-converting your decimals but it's not helping you to learn what it is going on...

例如,您开始的两个字节值是120 -38,但以十六进制表示的是0x78 0xDA.
现在,如果您通过Google搜索字节0x78 0xDA ,您会发现这两个字节是ZLIB的DEFLATE压缩算法的标头.

For example your beginning two byte values are 120 -38 but in hex that is 0x78 0xDA.
Now if you google search bytes 0x78 0xDA you will find out those two bytes are header for ZLIB's DEFLATE compression algorithm.

此ZLIB详细信息对于下一步非常重要...

This ZLIB detail is important to know for the next step...

(3)
有时,变量名在反编译期间并不总是恢复.这就是为什么您的所有代码都将这些愚蠢的_loc_作为通用名称(真正的var名称是未知的,只有它们的数据类型).

(3)
Sometimes the variable names are not always recovered during de-compiling. This is why all your code has these silly _loc_ as generic names (real var names are unknown, only their data type).

您的_loc1_.uncompress();应该包含指定算法的String变量.

Your _loc1_.uncompress(); is supposed to contain a String variable specifying the algorithm.

public function uncompress(algorithm:String) :void//来自AS3文档

public function uncompress(algorithm:String) :void //from AS3 documentation

在反编译期间,重要信息丢失了.幸运的是,只有3个选项"ZLIB","DEFLATE"或"LZMA".通过上面的通知(2),我们可以看到它应该为_loc1_.uncompress("DEFLATE");

During decompilation that important info was lost. Luckily there only 3 options "ZLIB", "DEFLATE" or "LZMA". From the above notice (2) we can see it should be _loc1_.uncompress("DEFLATE");

解决方案:

创建一个字节数组(不是Memory Stream),并手动填充十六进制值(例如: -13写为0xDA).

Create a byte array (not Memory Stream) and manually fill with hex values (eg: -13 is written 0xDA).

首先将您的每个数字转换为十六进制.您可以在Programmer模式下(在View选项下)使用Windows计算器,在该模式下,您可以在 dec 模式下键入小数,然后按 hex 以查看与十六进制格式相同的值. .也许某些在线工具也可以做到这一点.

First convert each of your numbers to hex. You can use Windows Calculator in Programmer mode (under View option), where you type a decimal in dec mode then press hex to see same value as hex format. Maybe some online tool can do it too.

最终的十六进制值应类似于78 DA 63 10 0C 33 29 8A 0C 32 51 49 31 C8 .... etc,直到结束的十六进制值F0等于结束的十进制-16.

The final hex values should look like 78 DA 63 10 0C 33 29 8A 0C 32 51 49 31 C8 .... etc until the ending hex value F0 which equals your ending decimal -16.

那您就可以轻松地做...

Then you can easily do...

public string p()
{

    byte[] loc_Data = new byte[] {
    0x78, 0xDA, 0x63, 0x10, 0x0C, 0x33, 0x29, 0x8A, 
    0x0C, 0x32, 0x51, 0x49, 0x31, 0xC8, 0x0D, 0x30, etc etc ... until 0xF0
    };

    var loc_Uncompressed = Ionic.Zlib.ZlibStream.UncompressBuffer( loc_Data );
    return Encoding.UTF8.GetString( loc_Uncompressed ); //or try: loc_Uncompressed.ToArray()

}

这篇关于什么是"ByteArray.uncompress()"?在AS3中等效于C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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