将c ++运算符翻译成VB.NET [英] Translation c++ operators to VB.NET

查看:105
本文介绍了将c ++运算符翻译成VB.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我对c ++运算符不是很熟练,所以我需要一些从c ++到vb.net的翻译帮助。



原始c ++代码是

Hi everyone,

I'm not very skilled with c++ operators, so I need some help with a translation from c++ to vb.net.

Original c++ code is

byte[] Dec = new byte[Enc.Length];
Dec[0] = Enc[0]; //Packet length
Dec[1] = (byte)((~Enc[1]) ^ 0x89);
int j;
for (j = 2; j < Dec[0]; j++)
{
    Dec[j] = (byte)((Enc[j-1] + 0xdc) ^ Enc[j]);
}
Dec[j] = (byte)(Enc[j] ^ Dec[2]);





其他地方我发现这个代码片段是另一种选择:



Somewhere else I found this snippet as an alternative:

dec[0] = enc[0];
dec[1] = (~enc[1]) ^ 0x89;
for (l=2; l < n-3; l++) dec[l] = (enc[l-1] + 0xdc) ^ enc[l];
dec[l] = enc[l] ^ dec[2];





我的翻译尝试是



My translation try was

Public Function TextStringToByteArray(ByRef str As String) As Byte()
    Dim enc As System.Text.Encoding = System.Text.Encoding.Default

    Return enc.GetBytes(str)
End Function
Public Function ByteArrayToTextString(ByRef Barr() As Byte) As String
    Dim enc As System.Text.Encoding = System.Text.Encoding.Default

    Return enc.GetString(Barr)
End Function

Private Function DecodePacket(ByVal Data As String) As String
    Dim dec(Data.Length) As Byte
    Dim enc() As Byte = TextStringToByteArray(Data)

    dec(0) = enc(0)                                     'dec[0] = enc[0];
    dec(1) = (Not enc(1)) ^ Hex(89)                     'dec[1] = (~enc[1]) ^ 0x89;

    Dim i As Integer
    For i = 2 To i < Data.Length - 3                    'for (l=2; l < n-3; l++)
        dec(i) = (enc(i - 1) + Hex("dc")) ^ enc(i)      'dec[l] = (enc[l-1] + 0xdc) ^ enc[l];
    Next

    dec(i) = enc(i) ^ dec(2)                            'dec[l] = enc[l] ^ dec[2];

    Return ByteArrayToTextString(dec)
End Function





但是使用代码会导致算术溢出。

因为代码对字节流进行了一些简单的解码(我还不知道原始字节流中有什么),我无法验证是否因为解码/代码转换错误或者流数据本身存在问题而导致溢出。



任何人都可以确认我的翻译是否正确?



非常感谢您提前

Phil



But using the code results in an arithmetic overflow.
Because the code does some simple decoding of a byte stream (and I do not know what is in the original byte stream yet), I can not verify if the overflow comes up because the decoding / code translation is wrong or because there is some problem with the streamed data itself.

Anyone who can confirm that my translation is correct?

Thank you very much in advance
Phil

推荐答案

看你的翻译太无聊了,所以我可以给你一个纯粹实用的建议:自动翻译。



关键是:对于两种不同的语言,您的代码将完全相同:C ++和C ++ / CLI。请执行以下操作:

It would be too boring to look at your translation, so I can give you one purely practical advice: do the translation automatically.

The key is: your code would be perfectly the same for two different languages: C++ and C++/CLI. So do the following:
  1. 在Visual Studio中创建C ++ / CLI(纯托管)项目,并将您的代码片段置于其中一个托管(ref)类型中: struct
  2. 构建此代码并在输出中获取.NET程序集。
  3. 下载开源ILSpy。您甚至可以自己构建它:

    http://ilspy.net [ ^ ],

    https ://github.com/icsharpcode/ILSpy [ ^ ] 。

  4. 在ILSpy中打开C ++ / CLI程序集并在输出时生成VB.NET代码。您不必翻译整个程序集。或者,您可以浏览它,只选择您需要的方法,并将其翻译成您需要的语言。您可以选择IL,C#和VB.NET之间的输出语言。
  5. 利润!
  1. Create a C++/CLI (purely managed) project in Visual Studio and put your fragment of code in one of the managed ("ref") types: a class or struct.
  2. Build this code and obtain a .NET assembly on output.
  3. Download open-source ILSpy. You can even build it yourself:
    http://ilspy.net[^],
    https://github.com/icsharpcode/ILSpy[^].
  4. Open your C++/CLI assembly in ILSpy and generated VB.NET code on output. You don't have to translate the whole assembly. Alternatively, you can browse it, choose only the method you need and see its translation into the language you need. You choose output language between IL, C# and VB.NET.
  5. PROFIT!





享受。这个想法在许多其他情况下运作良好;翻译代码的质量非常好。



-SA


行中的溢出:

The overflow in lines:
dec(1) = (Not enc(1)) ^ Hex(89)                     'dec[1] = (~enc[1]) ^ 0x89;
...
    dec(i) = (enc(i - 1) + Hex("dc")) ^ enc(i)      'dec[l] = (enc[l-1] + 0xdc) ^ enc[l];
...
dec(i) = enc(i) ^ dec(2)                            'dec[l] = enc[l] ^ dec[2];



因为VB中的C Xor运算符'^'意味着...的功率...



但我认为还有一个非常基本的错误:你认为你的输入流是一个字符串。

使用System.Text.Encoding.Default你根据当前的转换代码页,但如果您的流是二进制,那么您将获得错误的数据(System.Text.Encoding屏蔽MSB位或以其他方式更改值)。

您应该将程序或多或少编码为:


because the C Xor operator '^' in VB means power of...

But I think there is also a very basic error: you consider your input stream as a string.
Using System.Text.Encoding.Default you make a conversion based on the current codepage, but if your stream is a binary one you'll get wrong data (System.Text.Encoding masks MSB bit or alterate other way the values).
You should code your program more or less as:

Public Function DecodeBytePacket(Enc As Byte()) As Byte()
    Dim Dec As Byte() = New Byte(Enc.Length) {}
    Dec(0) = Enc(0)
    Dec(1) = (Not Enc(1) Xor 137)
    Dim i As Integer
    i = 2
    While i < CInt(Dec(0))
        Dec(i) = Convert.ToByte(((Convert.ToUInt32(Enc(i - 1)) + 220) Xor Convert.ToUInt32(Enc(i))) And 255)
        i = i + 1
    End While
    Dec(i) = (Enc(i) Xor Dec(2))
    DecodeBytePacket = Dec
End Function





审核并检查此例程的输出是否与C版本相同。



reviewed and checked the output of this routine is the same of the C version.


这篇关于将c ++运算符翻译成VB.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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