3 Replies - 3407 Views - Last Post: 02 June 2011 - 08:33 AM

#1 AdamSpeight2008   User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Get an hashcode for a String

Posted 10 July 2008 - 05:02 AM

Description: Example
IF GetHasCodeFromString(InputPassword$) = PasswordHashcode Then
' password was correct
else
' password failed
end if

Public Function GetHasCodeFromString(ByRef Key$) as long
' This function generates an hashcode for a string.
' F(n)=((127 & F)n-1) + Key$(n)) Mod 16908799
GetHashcodeFromString=0
Dim i as long
Dim j as long
l=len(key$)
If l>=0 then
 i =0
 while i
						
						

Is This A Good Question/Topic? 0
  • +

Replies To: Get an hashcode for a String

#2 1asolutions   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 02-June 11

Re: Get an hashcode for a String

Posted 02 June 2011 - 06:51 AM

Great snippet! Just a minor correction, based on my experiment using VBA for MS Access 2007. In this version of VBA (and chances are that it happens in many other versions of VBA) the snippet returns 0. This is due to the usage of the name of the function as a local variable. Use this version to fix the problem: Public Function GetHasCodeFromString(ByRef Key$) As Long ' This function generates a hashcode for a string. ' F(n)=((127 & F)n-1) + Key$(n)) Mod 16908799 Dim i As Long Dim j As Long Dim temp As Long Dim l As Long 'It could be integer if strings are short. l = Len(Key$) If l >= 0 Then i = 0 While i < l i = i + 1 temp = ((127 * temp) + Asc(Mid$(Key$, i, 1))) Mod 16908799 Wend End If GetHasCodeFromString = temp Debug.Print "Final result: Key = " & Key$ & "; Hashcode = " & temp End Function
Was This Post Helpful? 0
  • +
  • -

#3 1asolutions   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 02-June 11

Re: Get an hashcode for a String

Posted 02 June 2011 - 07:13 AM

(Hopefully it looks better this time)

Great snippet! Just a minor correction, based on my experiment using VBA for MS Access 2007. In this version of VBA (and chances are that it happens in many other versions of VBA) the snippet returns 0. This is due to the usage of the name of the function as a local variable. Use this version to fix the problem:

Public Function GetHasCodeFromString(ByRef Key$) As Long

'This function generates a hashcode for a string.

' F(n)=((127 & F)n-1) + Key$(n)) Mod 16908799

  Dim i As Long

  Dim j As Long

  Dim temp As Long

  Dim l As Long

  l = Len(Key$)

  If l >= 0 Then

    i = 0

    While i < l

      i = i + 1

      temp = ((127 * temp) + Asc(Mid$(Key$, i, 1))) Mod 16908799

    Wend

  End If

  GetHasCodeFromString = temp

  'Debug.Print "Final result: Key = " & Key$ & "; Hashcode = " & temp

End Function


Was This Post Helpful? 0
  • +
  • -

#4 AdamSpeight2008   User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: Get an hashcode for a String

Posted 02 June 2011 - 08:33 AM

vb6 the Function Name is used as a variable by which values are returned to the caller.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1