473,403 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

FNV1 Algorithm giving Arithmetic Operation Overflow

I am trying to make a small programm that will convert a given input string to a FNV1 32bit hash code. I found a function algorithm to do the actual convertion.
Expand|Select|Wrap|Line Numbers
  1. public static uint ComputeHash(IEnumerable<byte> bytes)
  2. {
  3.    uint hash = 2166136261;
  4.    foreach(byte b in bytes)
  5.    {
  6.       hash *= 16777619;
  7.       hash ^= b;
  8.    }
  9.    return hash;
  10.  
From what I have been able to read about and comparing to similar functions, this appears to be correct. But no matter what I put into the function I get a "arithmetic operation resulted in an overflow" OverflowException evertime, I am putting in a byte array equivelent of the input string. How do I fix this issue ? Am I just misunderstanding something or is the function faulty(more likely the former) ?
Nov 18 '11 #1

✓ answered by GaryTexmo

So tbHashin is a textbox on your form and you're just passing the text through? What kind of things are you testing with?

Can you try copy/pasting the code I wrote (into a new Console Application) and seeing if that works? I did try some longer strings, but nothing over 200 characters.

Looking at the code, that you get an overflow exception isn't that surprising because you keep multiplying by 16777619, but I looked at it and the bits just overflow and reset to a lower number. There is a way to force overflow checking though... and it's also a project setting. The first thing to check is your project's properties... click the Build tab, scroll down and click the Advanced button. Uncheck Check for arithmetic overflow/underflow if it is checked.

Alternatively, if you want to leave that setting enabled, you can specifically check or not check commands by putting them in checked or unchecked blocks. So for your function there, put the whole thing in something like this...

Expand|Select|Wrap|Line Numbers
  1. unchecked
  2. {
  3.   // Your code here
  4. }

5 3026
GaryTexmo
1,501 Expert 1GB
Can you post your calling code too? I just duplicated this and tried it with a few strings and am not getting the error.

Expand|Select|Wrap|Line Numbers
  1. class Program
  2. {
  3.     static void Main(string[] args)
  4.     {
  5.         Console.WriteLine(GetHash("abcd".Select(x => (byte)x).ToArray()));
  6.     }
  7.  
  8.     static uint GetHash(IEnumerable<byte> bytes)
  9.     {
  10.         uint hash = 2166136261;
  11.  
  12.         foreach (byte b in bytes)
  13.         {
  14.             hash *= 16777619;
  15.             hash ^= b;
  16.         }
  17.  
  18.         return hash;
  19.     }
  20. }
Nov 18 '11 #2
For now I have window form with 2 textboxes
  • tbHashIn
  • tbHashOut
And a button. In the buttonclick even is the following
Expand|Select|Wrap|Line Numbers
  1. string inputString = this.tbHashIn.text;
  2. byte[] inputBytes = new byte[inputString.Length];
  3. For (int i=0;i<inputBytes.Length;i++)
  4. {
  5.    inputBytes[i] = (byte)inputString[i];
  6. }
  7. uint outint = ComputeHash(inputBytes);
  8. this.tbHashOut.Text=outint.ToString(); 
Nov 19 '11 #3
sorry typo line 1 is
Expand|Select|Wrap|Line Numbers
  1. string inputString =this.tbHashin.Text;
Nov 19 '11 #4
GaryTexmo
1,501 Expert 1GB
So tbHashin is a textbox on your form and you're just passing the text through? What kind of things are you testing with?

Can you try copy/pasting the code I wrote (into a new Console Application) and seeing if that works? I did try some longer strings, but nothing over 200 characters.

Looking at the code, that you get an overflow exception isn't that surprising because you keep multiplying by 16777619, but I looked at it and the bits just overflow and reset to a lower number. There is a way to force overflow checking though... and it's also a project setting. The first thing to check is your project's properties... click the Build tab, scroll down and click the Advanced button. Uncheck Check for arithmetic overflow/underflow if it is checked.

Alternatively, if you want to leave that setting enabled, you can specifically check or not check commands by putting them in checked or unchecked blocks. So for your function there, put the whole thing in something like this...

Expand|Select|Wrap|Line Numbers
  1. unchecked
  2. {
  3.   // Your code here
  4. }
Nov 21 '11 #5
the text box entries are only about 20-30 characters on average with a max of about 40. I wrapped the code in the get hash function in a unchecked block and the program now seems to be working fine without any crashing (I forgot about those). Now I just need to find a way to confirm the hash ouput is correct (I'll check the net).
Thanks again for all your help on what should have been a simple small program (I've made larger more complex ones). It's been educational.
Thanks
CDK
Nov 21 '11 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Codemonkey | last post by:
Hi, When I first installed Visual Studio 2003, I noticed I was getting the follow error when showing a form: A first chance exception of type 'System.ArithmeticException' occurred in...
3
by: Unknown | last post by:
Why does this overflow? long testmath = (898 * 360 + 30) * 24 * 60 * 60 * 1000 / 25; but if I do it this way it works.. long testmath1 = (898 * 360 + 30) long testmath2 = testmath1 * 24 *...
3
by: Ghost | last post by:
currently new C only up to 64 bit operation, how can I perform operation with 100 or more digits? using stack? if use stack, how to write operation for +, -, *, / ? anyone can help?
4
by: glenn | last post by:
I have a COM Server I've written in C#. I have a client app I've written in Delphi that is calling the C# COM Server. However, one of the functions in the COM Server creates a form and during the...
4
by: Tom | last post by:
I have a VB.NET framework 1.1 application that I am installing on my user's workstation. It works fine on EVERY machine except for one - on this one machine it generates a 'Overflow or underflow in...
4
by: Thomas Kreuzer | last post by:
Hello everyone, I have a question regarding how to interpret a string. Basically I want to write a little calculator, I will type something like "12+69*12-44/2" and then want my program to...
2
by: Diego F. | last post by:
Hello. I'm having an error in a database access. I'm just making a select query to a table and using a DataAdapter to Fill a DataSet. Sometimes it works and sometimes it results in an exception:...
1
by: thebigsquid | last post by:
hi, its me again, so this software obviously has serious problems. now i'm getting this error message when i try to access any of it! any ideas much appreciated thanks the big squid An...
7
by: Mike TI | last post by:
Nov 13, 2007 Hi all Can I build a string for arithmetic operation. (VB NET 2005) e.g. I want to build an arithmetic operation from within the program as
2
by: vertigo262 | last post by:
Anyone know why this does this? Server Error in '/' Application. Arithmetic operation resulted in an overflow. <asp:Label ID="Label4" runat="server" Font-Size="Medium" Text='<%#...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.