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

captchaman

Junior Member
Joined
Sep 16, 2010
Messages
192
Reaction score
848
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.
 
Maybe if you post the C# code it would be easier to understand..
 
make its DLL and use it in Vb.net Project

or

Please Post the code so we can take a look :)
 
Post or PM your original C# then we could convert it for you
 
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
 
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.
 
Have you tried declaring k as an unsigned int?
 
Yeah, I was going to suggest declaring k as a long word rather than an int or unsigned int
 
Back
Top