Handling Unsigned Integers for Compatability
-
I am currently working on an Android application (my first) and I am trying to add the licensing code that I use for all my other applications. The issue I have run into is that all my other programs were written in VB.NET and there is a hashing function that uses
UInteger
. I recently discovered that Java doesn't have unsigned integers so my hash function in Android is failing to give the same results as my .NET equivalent. Unfortunately I've used this code in too many projects to go change the hashing routine in all my other applications. The issue is occurring in this piece of code:for(byte b : uniBytes){
hash += b;
hash += (hash << 10); <--This returns negative numbers
hash ^= (hash >>> 6);
}I tried implementing some logic so that after every operation the code would check if the
hash
variable was negative and if so convert it to a long such aslong lngHash = 0xFFFFFFFFL & hash;
, and I understand why it must be converted to a long. However, after a few more operations the hash would start to become incorrect again. I am assuming this is because if the number reaches a range that can fit into an integer, I must convert the number back to an integer in the same way when it grew I needed to convert it to an long. I have even tried some open source libraries but they didn't support the shift operators. Any suggestions or insights into this? Am I at least on the right track with having to convert the hash back and forth between an integer and long as need? Any help or guidance of how to get this method to work is greatly appreciated. Thanks.A black hole is where God tried to divide by zero. There are 10 kinds of people in the world; those who understand binary and those who don't.