locked
C# Conversion to VB RRS feed

  • Question

  • I am trying to coinvert the following code snippet from C# to VB. 

     

    C#

    //Write all data to the crypto stream and flush it.
    // Encode length as first 4 bytes
    byte[] length = new byte[4];
    length[0] = (byte)(message.Length & 0xFF);
    length[1] = (byte)((message.Length >> 8) & 0xFF);
    length[2] = (byte)((message.Length >> 16) & 0xFF);
    length[3] = (byte)((message.Length >> 24) & 0xFF);
    csEncrypt.Write(length, 0, 4);
    csEncrypt.Write(toEncrypt, 0, toEncrypt.Length);
    csEncrypt.FlushFinalBlock();

     

    I can't seem to convert the lines with the following syntax via the converter at (http://www.developerfusion.co.uk/Utilities/convertcsharptovb.aspx).  I am getting an error saying " The number style AllowHexSpecifier is not supported on floating point data types."  Assuming its the "& 0xFF" at the end of the line as when I remove it, it translates.  Why does this not translate?

     

    length[0] = (byte)(message.Length & 0xFF);
    length[1] = (byte)((message.Length >> 8) & 0xFF);
    length[2] = (byte)((message.Length >> 16) & 0xFF);
    length[3] = (byte)((message.Length >> 24) & 0xFF);

     

    VB Conversion

    Private length As Byte() = New Byte(4) {}

     

    'If I remove the "0XFF" I get the following:

    length(0) = CType((message.Length), Byte)
    length(1) = CType(((message.Length >> 8)), Byte)
    length(2) = CType(((message.Length >> 16)), Byte)
    length(3) = CType(((message.Length >> 24)), Byte)

     

    csEncrypt.Write(length, 0, 4)
    csEncrypt.Write(toEncrypt, 0, toEncrypt.Length)
    csEncrypt.FlushFinalBlock

     

    Thanks

     

    EM

    Thursday, May 17, 2007 2:59 PM

Answers

  • This:

    Code Snippet

    byte[] length = new byte[4];
    length[0] = (byte)(message.Length & 255);
    length[1] = (byte)((message.Length >> 8) & 255);
    length[2] = (byte)((message.Length >> 16) & 255);
    length[3] = (byte)((message.Length >> 24) & 255);
    csEncrypt.Write(length, 0, 4);
    csEncrypt.Write(toEncrypt, 0, toEncrypt.Length);
    csEncrypt.FlushFinalBlock();

     

    converts to this:

    Code Snippet

    Dim length(4) As Byte
    length(0) = CType((message.Length And 255), Byte)
    length(1) = CType(((message.Length >> 8) And 255), Byte)
    length(2) = CType(((message.Length >> 16) And 255), Byte)
    length(3) = CType(((message.Length >> 24) And 255), Byte)
    csEncrypt.Write(length, 0, 4)
    csEncrypt.Write(toEncrypt, 0, toEncrypt.Length)
    csEncrypt.FlushFinalBlock

     

    It only accepts decimal values.

    Thursday, May 17, 2007 9:16 PM
  • (via Instant VB)

    Note that the array upper bound is specified in VB, while the length is used in C#.

     

    Code Snippet

    'Write all data to the crypto stream and flush it.
    ' Encode length as first 4 bytes
    Dim length As Byte() = New Byte(3){}
    length(0) = CByte(message.Length And &HFF)
    length(1) = CByte((message.Length >> 8) And &HFF)
    length(2) = CByte((message.Length >> 16) And &HFF)
    length(3) = CByte((message.Length >> 24) And &HFF)
    csEncrypt.Write(length, 0, 4)
    csEncrypt.Write(toEncrypt, 0, toEncrypt.Length)
    csEncrypt.FlushFinalBlock()

     

    David Anton
    www.tangiblesoftwaresolutions.com
    Instant C#: VB to C# converter
    Instant VB: C# to VB converter
    C++ to C# Converter: converts C++ to C#
    Instant C++: converts C# to C++/CLI and VB to C++/CLI
    Instant Python: converts C# to IronPython and VB to IronPython
    Thursday, May 17, 2007 11:49 PM

All replies

  • This:

    Code Snippet

    byte[] length = new byte[4];
    length[0] = (byte)(message.Length & 255);
    length[1] = (byte)((message.Length >> 8) & 255);
    length[2] = (byte)((message.Length >> 16) & 255);
    length[3] = (byte)((message.Length >> 24) & 255);
    csEncrypt.Write(length, 0, 4);
    csEncrypt.Write(toEncrypt, 0, toEncrypt.Length);
    csEncrypt.FlushFinalBlock();

     

    converts to this:

    Code Snippet

    Dim length(4) As Byte
    length(0) = CType((message.Length And 255), Byte)
    length(1) = CType(((message.Length >> 8) And 255), Byte)
    length(2) = CType(((message.Length >> 16) And 255), Byte)
    length(3) = CType(((message.Length >> 24) And 255), Byte)
    csEncrypt.Write(length, 0, 4)
    csEncrypt.Write(toEncrypt, 0, toEncrypt.Length)
    csEncrypt.FlushFinalBlock

     

    It only accepts decimal values.

    Thursday, May 17, 2007 9:16 PM
  • (via Instant VB)

    Note that the array upper bound is specified in VB, while the length is used in C#.

     

    Code Snippet

    'Write all data to the crypto stream and flush it.
    ' Encode length as first 4 bytes
    Dim length As Byte() = New Byte(3){}
    length(0) = CByte(message.Length And &HFF)
    length(1) = CByte((message.Length >> 8) And &HFF)
    length(2) = CByte((message.Length >> 16) And &HFF)
    length(3) = CByte((message.Length >> 24) And &HFF)
    csEncrypt.Write(length, 0, 4)
    csEncrypt.Write(toEncrypt, 0, toEncrypt.Length)
    csEncrypt.FlushFinalBlock()

     

    David Anton
    www.tangiblesoftwaresolutions.com
    Instant C#: VB to C# converter
    Instant VB: C# to VB converter
    C++ to C# Converter: converts C++ to C#
    Instant C++: converts C# to C++/CLI and VB to C++/CLI
    Instant Python: converts C# to IronPython and VB to IronPython
    Thursday, May 17, 2007 11:49 PM