1

I need the following function converted to VB.NET, but I'm not sure how to handle the statement

res = (uint)((h * 0x21) + c);

Complete function:

private static uint convert(string input)
{
    uint res = 0;
    foreach (int c in input)
        res = (uint)((res * 0x21) + c);
    return res;
}

I created the following, but I get an overflow error:

Private Shared Function convert(ByVal input As String) As UInteger

    Dim res As UInteger = 0
    For Each c In input
        res = CUInt((res * &H21) + Asc(c)) ' also tried AscW 
    Next
    Return res

End Function

What am I missing? Can someone explain the details?

2
  • What inputs are failing? Are you sure you're not passing an input that is too large for a UInt32? Commented Jul 18, 2012 at 11:28
  • 1
    I think the string is causing it to flow over the uint limits Commented Jul 18, 2012 at 11:30

2 Answers 2

3

Your code is correct. The calculation is overflowing after just a few characters since res increases exponentially with each iteration (and it’s not the conversion on the character that’s causing the overflow, it’s the unsigned integer that overflows).

C# by default allows integer operations to overflow – VB doesn’t. You can disable the overflow check in the project settings of VB, though. However I would try not to rely on this. Is there a reason this particular C# has to be ported? After all, you can effortlessly mix C# and VB libraries.

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

Comments

1

Here is a useful online converter: http://www.developerfusion.com/tools/convert/csharp-to-vb/

1 Comment

It should return the character code. That’s what the C# code does, too, only implicitly. Your code, on the other hand, won’t compile.

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.