Skip to content

Instantly share code, notes, and snippets.

@vaiorabbit
Last active December 17, 2015 19:49
Show Gist options
  • Save vaiorabbit/5663123 to your computer and use it in GitHub Desktop.
Save vaiorabbit/5663123 to your computer and use it in GitHub Desktop.
using System;
using System.IO; // TextReader,TextWriter,StreamReader,StreamWriter
using System.Text; // Encoding
using System.Text.RegularExpressions; // Regex, Match
/*
$ /Library/Frameworks/Mono.framework/Commands/mcs fnv32a.cs
$ /Library/Frameworks/Mono.framework/Commands/mono ./fnv32a.exe > tmp_cs.txt
*/
public class Hashcode
{
public static uint FNV1_32A_INIT = 0x811c9dc5;
public static uint FNV_32_PRIME = 0x01000193;
public static uint fnv32a( string str )
{
uint hval = FNV1_32A_INIT;
byte[] buf = Encoding.ASCII.GetBytes(str);
foreach ( byte b in buf )
{
hval ^= b;
hval *= FNV_32_PRIME;
}
return hval;
}
public static void Main()
{
TextReader instrm = new StreamReader(Console.OpenStandardInput());
while ( instrm.Peek() >= 0 )
{
string line = instrm.ReadLine();
Match result = Regex.Match( line, "^\"(.*)\",$" );
if ( result.Success )
{
string word = result.Groups[1].Value;
Console.WriteLine("{0} -> {1}", word, fnv32a(word));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment