File Exchange

image thumbnail

String2Hash

version 1.0.0.0 (1.63 KB) by Dirk-Jan Kroon
Convert a String Array (Text) to a hash code

6 Downloads

Updated 18 Jun 2010

View License

This function generates a hash value from a text string

hash=string2hash(str,type);

inputs,
str : The text string, or array with text strings.
outputs,
hash : The hash value, integer value between 0 and 2^32-1
type : Type of has 'djb2' (default) or 'sdbm'

From c-code on : http://www.cse.yorku.ca/~oz/hash.html

djb2
this algorithm was first reported by dan bernstein many years ago
in comp.lang.c

sdbm
this algorithm was created for sdbm (a public-domain reimplementation of
ndbm) database library. it was found to do well in scrambling bits,
causing better distribution of the keys and fewer splits. it also happens
to be a good general hashing function with good distribution.

example,

hash=string2hash('hello world');
disp(hash);

Comments and Ratings (6)

stewpend0us

@Olaf Bousche (and anyone else who may be interested) yes you are correct. 2^32-1 should be 2^32 for sdbm

Song

Song

very good

Nice idea. SDBM is pretty good. I looked at the code: Since you are emulating c-code uint32, should you not be using

mod(....,2^32)

instead of

mod(....,2^32-1)

Please comment.

MATLAB Release Compatibility
Created with R2010a
Compatible with any release
Platform Compatibility
Windows macOS Linux