tag:blogger.com,1999:blog-8911723.post110856869467750554..comments2023-06-25T13:27:21.190+00:00Comments on DotNetYOGI: What is under the hood of .Net String.GetHashCode()?Rahul Kumarhttp://www.blogger.com/profile/11131035410609412406noreply@blogger.comBlogger3125tag:blogger.com,1999:blog-8911723.post-35308448703877390372007-08-25T21:23:00.000+00:002007-08-25T21:23:00.000+00:00This is what I came up with for VB.NET. But since...This is what I came up with for VB.NET. But since I needed to have compiler integer checks turned on, it's only the 24 least significant bits. Turn off compiler checks and remove the 24 bit mask for the full 32 bits.<BR/><BR/> Shared Function GetHashCode24Bit(ByVal text As String) As Integer<BR/> GetHashCode24Bit = 5381<BR/> Dim limit As Integer = text.Length - 1<BR/> Dim index As Integer<BR/> For index = 0 To limit<BR/> GetHashCode24Bit = (((GetHashCode24Bit * 32) + GetHashCode24Bit) Xor Convert.ToInt32(text.Chars(index))) And &HFFFFFF<BR/> Next<BR/>#If Debug Then<BR/> Dim textHashCodeDotNet1 As Integer = text.GetHashCode And &HFFFFFF<BR/> If GetHashCode24Bit <> textHashCodeDotNet1 Then<BR/> MsgBox("For text = " & text & ", GetHashCode24Bit = " & GetHashCode24Bit & ", but GetHashCode = " & textHashCodeDotNet1)<BR/> End If<BR/>#End If<BR/> End FunctionAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8911723.post-1152540075749348072006-07-10T14:01:00.000+00:002006-07-10T14:01:00.000+00:00That is very helpful.Is this possible to get this ...That is very helpful.Is this possible to get this code in VB.NET?<BR/><BR/>C#:<BR/>int HashString(string szStr)<BR/>{<BR/>int hash = 5381;<BR/>int c;<BR/>int i = 0;<BR/><BR/>while(i < szStr.Length)<BR/>{<BR/>c = (int)szStr[i];<BR/><BR/>hash = ((hash << 5) + hash) ^ c;<BR/>i++;<BR/>}<BR/>return hash;<BR/>}Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8911723.post-1146927158773918862006-05-06T14:52:00.000+00:002006-05-06T14:52:00.000+00:00home equity loanhome equity loanAnonymousnoreply@blogger.com