user1082391
user1082391

Reputation: 21

C# Function to Visual Basic 2010 Error with BitShift Operator "<<"

I am currently translating a C# code to Visual Basic 2010. Works good but now I dont get the correct line. everytime i got a bitshift error

Original Code looks like:

Code:

private static string Checksum(string url)
    {
        uint Magic = 0xE6359A60;

        uint a, b;
        uint c = Magic;

        a = b = 0x9E3779B9;

        int k = 0;
        int length = url.Length;

        //Algorithm
        while (length >= 12)
        {
            a += (uint)(url[k + 0] + (url[k + 1] << 8) + (url[k + 2] << 16) + (url[k + 3] << 24));
            b += (uint)(url[k + 4] + (url[k + 5] << 8) + (url[k + 6] << 16) + (url[k + 7] << 24));
            c += (uint)(url[k + 8] + (url[k + 9] << 8) + (url[k + 10] << 16) + (url[k + 11] << 24));

            Mix(ref a, ref b, ref c);

            k += 12;
            length -= 12;
        }

and my translation is:

Private Function Checksum(ByVal url) As String
    Dim Magic As UInteger = &HE6359A60UI

    Dim a As UInteger, b As UInteger
    Dim c As UInteger =Magic

    a = b = &H9E3779B9UI

    Dim k As Integer = 0
    Dim length As Integer = url.Length


    'Algorithm
    While length >= 12
        a += CUInt(url(k + 0) + (url(k + 1) << 8) + (url(k + 2) << 16) + (url(k + 3) << 24))
        b += CUInt(url(k + 4) + (url(k + 5) << 8) + (url(k + 6) << 16) + (url(k + 7) << 24))
        c += CUInt(url(k + 8) + (url(k + 9) << 8) + (url(k + 10) << 16) + (url(k + 11) << 24))
        Mix(a, b, c)

        k += 12
        length -= 12
    End While

But when the debugger comes to

a += CUInt(url(k + 0) + (url(k + 1) << 8) + (url(k + 2) << 16) + (url(k + 3) << 24))

it throw an exception:The operator "<<" is not defined for the Typ Char.

Upvotes: 2

Views: 228

Answers (2)

Guffa
Guffa

Reputation: 700592

In C# the compiler automatically converts from char to int. As the VB compiler doesn't do that, you have to do an explicit conversion:

a += CUInt(AscW((url(k + 0)) + (AscW(url(k + 1)) << 8) + (AscW(url(k + 2)) << 16) + (AscW(url(k + 3)) << 24))

Upvotes: 2

SSS
SSS

Reputation: 5403

a += CUInt(AscW(url(k + 0))) + (CUInt(AscW(url(k + 1))) << 8) + (CUInt(AscW(url(k + 2))) << 16) + (CUInt(AscW(url(k + 3))) << 24)

Assuming your characters are Unicode. If ASCII, use Asc() instead of AscW()

Upvotes: 1

Related Questions