问题描述
大家好,
我对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
推荐答案
- 在Visual Studio中创建C ++ / CLI(纯托管)项目,并将您的代码片段置于其中一个托管(ref)类型中:
类
或struct
。 - 构建此代码并在输出中获取.NET程序集。
- 下载开源ILSpy。您甚至可以自己构建它:
[],
[] 。
- 在ILSpy中打开C ++ / CLI程序集并在输出时生成VB.NET代码。您不必翻译整个程序集。或者,您可以浏览它,只选择您需要的方法,并将其翻译成您需要的语言。您可以选择IL,C#和VB.NET之间的输出语言。
- 利润!
- 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
orstruct
. - Build this code and obtain a .NET assembly on output.
- Download open-source ILSpy. You can even build it yourself:
http://ilspy.net[^],
https://github.com/icsharpcode/ILSpy[^]. - 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.
- PROFIT!
享受。这个想法在许多其他情况下运作良好;翻译代码的质量非常好。
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!