1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Arithmetic overflow [algorithm conversion from C# to VB.NET]

Discussion in 'Visual Basic .NET' started by captchaman, Nov 4, 2011.

  1. captchaman

    captchaman Junior Member

    Joined:
    Sep 16, 2010
    Messages:
    190
    Likes Received:
    842
    Occupation:
    Software Programmer
    Location:
    USA
    I'm trying to convert the Google PR checksum algorithm from C# to VB.NET, the converter has done what it can but I'm doing the rest manually. Here's the error dump:
    Code:
    System.OverflowException was unhandled
      Message=Arithmetic operation resulted in an overflow.
      Source=WindowsApplication4
      StackTrace:
           at WindowsApplication4.Form1.Crashit() in C:\Users\rx\AppData\Local\Temporary Projects\WindowsApplication4\Form1.vb:line 11
           at WindowsApplication4.Form1.Button1_Click(Object sender, EventArgs e) in C:\Users\rx\AppData\Local\Temporary Projects\WindowsApplication4\Form1.vb:line 24
           at System.Windows.Forms.Control.OnClick(EventArgs e)
           at System.Windows.Forms.Button.OnClick(EventArgs e)
           at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
           at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
           at System.Windows.Forms.Control.WndProc(Message& m)
           at System.Windows.Forms.ButtonBase.WndProc(Message& m)
           at System.Windows.Forms.Button.WndProc(Message& m)
           at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
           at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
           at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
           at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
           at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
           at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
           at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
           at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
           at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
           at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
           at WindowsApplication4.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
           at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
           at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
           at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           at System.Threading.ThreadHelper.ThreadStart()
      InnerException:
    
    Here's the code that causes the crash:
    Code:
        Function Crashit()
            Dim a As UInteger, b As UInteger
            Dim k As Integer = 0
            a = InlineAssignHelper(b, &H9E3779B9UI)
            Dim URL As String = "info:google.com"
            Dim length As Integer = URL.Length
            While length >= 12
                a += CUInt(AscW(URL(k + 0))) + CUInt(AscW(URL(k + 1)) << 8) + CUInt(AscW(URL(k + 2)) << 16) + CUInt(AscW(URL(k + 3)) << 24)
            End While
        End Function
        Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, value As T) As T
            target = value
            Return value
        End Function
    The offending code is
    Code:
    CUInt(AscW(URL(k + 3)) << 24)
    (Last part in the a calculation)

    Here's the same code (from the original working source) in C#:
    Code:
                    uint a, b;
                    a = b = 0x9E3779B9;
                    int k = 0;
                    string URL = "info:google.com";
                    int length = URL.Length;
                    while (length >= 12)
                    {
                        a += (uint)(URL[k + 0] + (URL[k + 1] << 8) + (URL[k + 2] << 16) + (URL[k + 3] << 24));
                    }
    So, what's the deal? I've been trying to fix it but to no avail.
     
  2. izSpK

    izSpK BANNED BANNED

    Joined:
    Sep 5, 2010
    Messages:
    1,546
    Likes Received:
    1,666
    Maybe if you post the C# code it would be easier to understand..
     
  3. shubhamm

    shubhamm Junior Member

    Joined:
    Jan 25, 2010
    Messages:
    107
    Likes Received:
    25
    Occupation:
    Developer
    Location:
    BHW
    make its DLL and use it in Vb.net Project

    or

    Please Post the code so we can take a look :)
     
  4. healzer

    healzer Jr. Executive VIP Jr. VIP Premium Member

    Joined:
    Jun 26, 2011
    Messages:
    2,361
    Likes Received:
    1,966
    Gender:
    Male
    Occupation:
    Marketing automation tools
    Location:
    Somewhere in Europe
    Home Page:
    Post or PM your original C# then we could convert it for you
     
  5. christoss1959

    christoss1959 Senior Member

    Joined:
    Nov 25, 2010
    Messages:
    894
    Likes Received:
    1,150
    Home Page:
    Had the same problem as you and could find no answer. I ended up compiling the dll in C# and calling it in vb.net
     
  6. yashachan

    yashachan Newbie

    Joined:
    Jan 11, 2012
    Messages:
    7
    Likes Received:
    0
    That usually happens whenever your declared variable cannot sustain the amount of length given. For example if you have an Integer and you set 274837492958235 to it it will crash and give an arithmetic overflow. BUT! if you declare as "Long" then that giant number will fit and you shouldn't have that issue anymore.
     
  7. Chris22

    Chris22 Regular Member

    Joined:
    Sep 29, 2010
    Messages:
    400
    Likes Received:
    1,059
    Have you tried declaring k as an unsigned int?
     
  8. Choronzon

    Choronzon Power Member

    Joined:
    Apr 20, 2008
    Messages:
    559
    Likes Received:
    376
    Yeah, I was going to suggest declaring k as a long word rather than an int or unsigned int
     
  9. memme

    memme Jr. VIP Jr. VIP Premium Member

    Joined:
    Sep 19, 2009
    Messages:
    1,168
    Likes Received:
    99
    Location:
    Blackhatlinks.com
    Home Page: