How to create the DJB2 hash function for strings in C#

1 Answer

0 votes
using System;

public class Program
{
	 public static int DJB2(string str) {
		int hash = 5381;

		for (int i = 0; i < str.Length; i++) {
			hash = ((hash << 5) + hash) + str[i];
		}

		return hash;
	 }

	public static void Main(string[] args)
	{
		int hash = DJB2("c# .net");

		Console.WriteLine(hash);
	}
}




/*
run:
          
478663936
  
*/

 



answered Oct 8, 2023 by avibootz

Related questions

1 answer 145 views
2 answers 112 views
1 answer 127 views
1 answer 121 views
1 answer 18 views
...