djb2 Hash Calculator (djb2, sdbm, Java)

Calculate the classic non-cryptographic string hashes that appear all over textbooks and standard libraries: Dan Bernstein's djb2 and its XOR variant djb2a, the sdbm hash, and Java's String.hashCode. Each is shown in hexadecimal, unsigned decimal, and signed 32-bit decimal so you can match whatever your language reports. Everything runs live in your browser.

How to use the djb2 Hash Calculator (djb2, sdbm, Java)

Type or paste into the input and choose Text or Hex bytes. In hex mode you can separate bytes with spaces or commas and use an optional 0x prefix; in text mode the characters are converted to UTF-8 bytes first. The table updates live and shows, for each algorithm, the 32-bit result in hexadecimal, unsigned decimal, and signed decimal.

The default input hello lets you confirm the values: djb2 gives 261238937 and Java\'s String.hashCode gives 99162322, both well-known constants. The signed column matters most for Java and C#, whose hash methods return a two\'s-complement int that is often negative; the unsigned column matches C, Go, and JavaScript implementations that keep the value in the 0 to 4294967295 range.

The calculation runs entirely in your browser, so it is instant, works offline, and nothing you enter is uploaded anywhere.

The classic string hashes and how they differ

Long before MurmurHash and xxHash, a handful of tiny, fast string hash functions became the standard way to turn a string into a bucket index for a hash table. They are non-cryptographic — built for speed and a reasonable spread, not for resisting attackers — and they are short enough to memorise, which is part of why they spread through Usenet, textbooks, and standard libraries. This tool computes the four you are most likely to encounter, all as 32-bit values.

djb2, attributed to Daniel J. Bernstein, starts the hash at the magic number 5381 and for each byte does hash = hash * 33 + byte. The multiplication by 33 is usually written as a shift-and-add ((hash << 5) + hash) because that was faster on old hardware. The closely related djb2a replaces the addition of the byte with an exclusive-or — hash = hash * 33 ^ byte — which mixes slightly differently and is the variant some libraries actually ship. sdbm, which came from the public-domain reimplementation of the ndbm database, uses hash = byte + (hash << 6) + (hash << 16) - hash; the odd-looking combination of shifts approximates a multiply by 65599 and tends to distribute well over text. Finally, Java\'s String.hashCode is specified by the language as hash = 31 * hash + char starting from zero, computed over UTF-16 code units, which is why every Java program agrees on it and why it appears in countless interview questions.

Because all four are defined by such simple recurrences, two correct implementations always produce the same number, and a single check value confirms a port instantly: djb2 of hello is 261238937 and Java\'s hashCode of hello is 99162322. The one subtlety that trips people up is signedness. The arithmetic naturally overflows 32 bits; C, Go, and JavaScript implementations usually mask the result back to an unsigned 32-bit integer, while Java and C# keep it as a signed int that can be negative. The bit pattern is identical either way — 0xF923099 is the same value whether you read it as 261238937 or, for results above two billion, as a negative number — which is exactly why this tool shows both so you can match your own platform without guessing.

Common use cases

  • Hash tables. Reproduce the bucket index a C or C++ program computes with djb2 or sdbm.
  • Java interop. Confirm a String.hashCode value, including the negative ones Java reports.
  • Teaching and interviews. Demonstrate how a few lines turn a string into a well-spread integer.
  • Porting code. Verify a reimplementation matches the reference with a known check value.

Frequently asked questions

Are these hashes secure?

No. djb2, sdbm, and String.hashCode are non-cryptographic hashes meant for hash tables and quick lookups, not security. They are easy to collide on purpose, so never use them for passwords, signatures, or tamper detection.

Why is the Java result sometimes negative?

Java's hashCode returns a signed 32-bit int, so values above about 2.1 billion wrap to negatives. The unsigned column shows the same bits as a 0-to-4294967295 number; only the interpretation differs.

What is the difference between djb2 and djb2a?

djb2 adds each byte (hash*33 + byte); djb2a exclusive-ors it (hash*33 ^ byte). The XOR variant mixes the low bits slightly differently. Both start from 5381 and both are widely used, so check which one your reference uses.

Does Java hashCode use bytes or characters?

It uses UTF-16 code units. For plain ASCII the code units equal the bytes, so the result matches a byte-wise computation; for non-ASCII text the UTF-16 units differ from UTF-8 bytes. This tool hashes the UTF-8 bytes of your input.