Results 1 to 2 of 2

Thread: GetHashCode 64 vs 32

  1. #1

    Thread Starter
    Frenzied Member mickey_pt's Avatar
    Join Date
    Sep 2006
    Location
    Corner of the Europe :)
    Posts
    1,959

    GetHashCode 64 vs 32

    Hello
    I have a very old app that uses a little function that calculates a value and after that i get the hash code of that value, that it's used as a password to unlock some functionalities to the user. Now the manager has a 64 bits computer and the function returns a different value.

    What i need it's how to convert this function from C# to Vb, i know that I can use the converts, but they don't work, pointers...

    c# Code:
    1. public static unsafe int GetHashCode32(string s)
    2.     {
    3.         fixed (char* str = s.ToCharArray())
    4.         {
    5.             char* chPtr = str;
    6.             int num = 0x15051505;
    7.             int num2 = num;
    8.             int* numPtr = (int*)chPtr;
    9.             for (int i = s.Length; i > 0; i -= 4)
    10.             {
    11.                 num = (((num << 5) + num) + (num >> 0x1b)) ^ numPtr[0];
    12.                 if (i <= 2)
    13.                 {
    14.                     break;
    15.                 }
    16.                 num2 = (((num2 << 5) + num2) + (num2 >> 0x1b)) ^ numPtr[1];
    17.                 numPtr += 2;
    18.             }
    19.             return (num + (num2 * 0x5d588b65));
    20.         }
    21.     }

    Can I convert this function?

    Rate People That Helped You
    Mark Thread Resolved When Resolved

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: GetHashCode 64 vs 32

    Try this (not 100&#37; sure it will work, you might need to use the Marshal object).
    Code:
    Public Shared Function GetHashCode32(s As String) As Integer
        Dim chPtr As Pointer(Of Char) = str
        Dim num As Integer = &H15051505
        Dim num2 As Integer = num
        Dim numPtr As Pointer(Of Integer) = CType(chPtr, Pointer(Of Integer))
        For i As Integer = s.Length To 1 Step -4
            num = (((num << 5) + num) + (num >> &H1b)) Xor numPtr(0)
            If i <= 2 Then
                Exit For
            End If
            num2 = (((num2 << 5) + num2) + (num2 >> &H1b)) Xor numPtr(1)
            numPtr += 2
        Next
        Return (num + (num2 * &H5d588b65))
    End Function

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width