Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
C++
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:
C++
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
VB
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


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
Posted
Comments
Philippe Mori 23-May-15 23:03pm    
I would think that the code should ignore overflow. I don't know how to do this in VB. In C++ (and in C#), overflow are ignored by default.
Sergey Alexandrovich Kryukov 23-May-15 23:12pm    
I suggested to perform automatic translation, which is really easy to get. Please see my answer.
—SA

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


Enjoy. This idea works well in many other cases; the quality of translated code is very good.

—SA
 
Share this answer
 
v2
The overflow in lines:
VB
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];

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


[EDIT] reviewed and checked the output of this routine is the same of the C version.
 
Share this answer
 
v9

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900