3

The following code in C# gives result "228452386"

UInt32 a;

int k = 0;

a = 0x9E3779B9;

a += (UInt32)(url[k + 0] + (url[k + 1] << 8) + (url[k + 2] << 16) + (url[k + 3] << 24));

After above code is ran, "a" contains "228452386".

But the following same code in VB.NET results in "Arithmetic operation resulted in an overflow". Basically the last statement is returning value "1868983913", so the runtime error is generated.

Dim a As UInt32

Dim k As Integer = 0

a = &H9E3779B9UI

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

Please note the variable "url" in the above code could be any string and it is same for both codes.

When I run the following statements in both C# and VB.NET than they both return same value

C#

(UInt32)(url[k + 0] + (url[k + 1] << 8) + (url[k + 2] << 16) + (url[k + 3] << 24))

VB.NET

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

Both statements return the value "1868983913" for "url" "info:microsoft.com". But when I run the complete statement with a += ......... then VB.NET gives an error while C# returns the value "228452386".

7

3 Answers 3

2

You have an overflow, because the result of adding the two numbers you're adding are larger than what fits in an UInt.

The resulting number is a 33 bit number, UInts fit only 32 bits.

  • In C#, the most significant bit is just chopped of, keeping the 32 bits that fit inside the UInt. (This new value is not the actual result of adding your numbers.)
  • In VB.NET you get an exception stating that the number doesn't fit.

The VB.NET compiler (or VS) allows you to turn of this overflow checking, so that VB.NET will behave like C# in this case.

Sign up to request clarification or add additional context in comments.

Comments

1

VB.NET automatically inserts tests for overflow, while C# doesn't. The equivalent C# code would be:

UInt32 a;
int k = 0;
a = 0x9E3779B9;
checked{
    a += (UInt32)(url[k + 0] + (url[k + 1] << 8) + (url[k + 2] << 16) + (url[k + 3] << 24));
}

EDIT: Changed code sample to comply with edited question.

3 Comments

Basically I want equivalent code for VB.Net based on C# code.
I didn't find the equivalent "unchecked" keyword for VB. (Don't know if it has.) But you can turn of overflow checks for your VB.NET application, as stated here: stackoverflow.com/a/4515123/19594
Please check my EDIT above as I have added more info.
0

Use the following VB.NET code:

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

AscW returns an Integer (signed) and what you need is unsigned.

EDIT

This works:

    Dim a As Int64
    Dim result As UInt32

    Dim k As Integer = 0

    Dim url As String = "info:microsoft.com"

    a = &H9E3779B9UI

    Dim a1 As UInt32 = 0
    Dim a2 As UInt32 = 0
    Dim a3 As UInt32 = 0
    Dim a4 As UInt32 = 0

    a1 = AscW(url(k + 0))
    a2 = AscW(url(k + 1)) << 8
    a3 = AscW(url(k + 2)) << 16
    a4 = AscW(url(k + 3)) << 24

    a = (a + a1 + a2 + a3 + a4) And &HFFFFFFF
    result = Convert.ToUInt32(a)

3 Comments

I got same error when I used the above statement because it is also giving value "1868983913" as stated in my original problem above.
It returned the same value on my PC, which string did you use for url?
Please check my EDIT above as I have added more info.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.