# Making a 6-digit numeric hash from a string?

**URL:** <https://boards.straightdope.com/t/making-a-6-digit-numeric-hash-from-a-string/637401>\
**Category:** Factual Questions\
**Created:** [October 11, 2012, 7:34am UTC](https://boards.straightdope.com/t/making-a-6-digit-numeric-hash-from-a-string/637401 "2012-10-11T07:34:57Z")\
**Posts on this page:** 9\
**Page:** 2

<div class="post-metadata">

**Author:** ![Reply](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/reply/32/15952_2.png) [@Reply](https://boards.straightdope.com/u/Reply)\
**Post date:** [October 11, 2012, 10:22pm UTC](https://boards.straightdope.com/t/making-a-6-digit-numeric-hash-from-a-string/637401/21 "2012-10-11T22:22:30Z")

</div>

> [@TriPolar](#):
>
> I am being facetious. It’s just an application of Murphy’s law.
> 
> However, I’d never use a hash without a collision detection system. I don’t particularly like hashing anyway because it doesn’t scale. I’m old enough to have seen memory grow from Kbytes to Tbytes so I’ve seen the hashing algorithms get reworked many times. I’m a Btree and Ptrie fan myself. But that’s a whole other holy war. The major feature of hashing is the time efficiency factor and I don’t see that in the OP’s application.

The big problem here is that something that should be trivial (generating unique indices for a set of records) is made impossible by a proprietary library software that uses its own database format. I have to work within that constraint until and unless we migrate to better library software (which definitely exist, but are much harder to use).

So in the meantime, I can’t trust our organization (a tiny nonprofit) to have the infrastructure and institutional memory to properly keep track of sequential unique IDs in a separate database.

The only other option I can think of – and you’re welcome to propose better solutions if you know of any – is using a timestamp for the ID. Unix time seems to be 10 digits for the next few centuries so that might work, but the ID# generation will happen in batches and over milliseconds, so I’d need to figure out how to get more precision than that while maintaining a fixed number of digits (10 or less) to maintain EAN-13 barcode compatibility.

Anyway, I think the hashes, while imperfect, seem like the least problematic at this point…

---

<div class="post-metadata">

**Author:** ![Chronos](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/chronos/32/134_2.png) [@Chronos](https://boards.straightdope.com/u/Chronos)\
**Post date:** [October 11, 2012, 10:25pm UTC](https://boards.straightdope.com/t/making-a-6-digit-numeric-hash-from-a-string/637401/22 "2012-10-11T22:25:25Z")

</div>

Timestamps also wouldn’t get you the same number for a title that was entered into the system twice.

---

<div class="post-metadata">

**Author:** ![Dr.Strangelove](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/dr.strangelove/32/6613_2.png) [@Dr.Strangelove](https://boards.straightdope.com/u/Dr.Strangelove)\
**Post date:** [October 12, 2012, 12:38am UTC](https://boards.straightdope.com/t/making-a-6-digit-numeric-hash-from-a-string/637401/23 "2012-10-12T00:38:46Z")

</div>

> [@TriPolar](#):
>
> I’m a Btree and Ptrie fan myself. But that’s a whole other holy war. The major feature of hashing is the time efficiency factor and I don’t see that in the OP’s application.

Oddly, I discovered tries (that looks weird… what do you call more than one trie?) on my own after finding myself dissatisfied with hashes. I had a college course which had a little competition to see who could write the fastest program do generate a word histogram from a text. Came up with the idea of a tree with one node per letter. The final alphabetic sort was trivial. Placed second out of a few hundred students.

Anyway, I’ve used hashes all the time without collision detection. Obviously it only works under certain circumstances, but there’s no problem with the concept in general, even in situations where a lot is on the line.

---

<div class="post-metadata">

**Author:** ![imageat](https://avatars.discourse-cdn.com/v4/letter/i/f17d59/32.png) [@imageat](https://boards.straightdope.com/u/imageat)\
**Post date:** [May 28, 2014, 2:05am UTC](https://boards.straightdope.com/t/making-a-6-digit-numeric-hash-from-a-string/637401/24 "2014-05-28T02:05:07Z")

</div>

right, there is no need to limit barcode to ISBN, EAN barcode can also scanned by fake [barcode reader utility in vb.net](http://www.keepdynamic.com/barcoding/vbnet-barcode-reader.shtml).

---

<div class="post-metadata">

**Author:** ![ZenBeam](https://avatars.discourse-cdn.com/v4/letter/z/3ab097/32.png) [@ZenBeam](https://boards.straightdope.com/u/ZenBeam)\
**Post date:** [May 28, 2014, 2:33am UTC](https://boards.straightdope.com/t/making-a-6-digit-numeric-hash-from-a-string/637401/25 "2014-05-28T02:33:10Z")

</div>

A year and a half late, but …

How will (did) you ensure the title is entered identically every time?

A Story About A Dog Vol 2. --\> 320812  
A Story About A Dog, Vol 2. --\> 563827  
A Story About A Dog Vol 2 --\> 478901

---

<div class="post-metadata">

**Author:** ![Reply](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/reply/32/15952_2.png) [@Reply](https://boards.straightdope.com/u/Reply)\
**Post date:** [May 28, 2014, 4:42am UTC](https://boards.straightdope.com/t/making-a-6-digit-numeric-hash-from-a-string/637401/26 "2014-05-28T04:42:33Z")

</div>

> [@ZenBeam](#):
>
> A year and a half late, but …
> 
> How will (did) you ensure the title is entered identically every time?
> 
> A Story About A Dog Vol 2. –\> 320812  
> A Story About A Dog, Vol 2. –\> 563827  
> A Story About A Dog Vol 2 –\> 478901

I stripped whitespaces, punctuation, and capitalization so that all three became “astoryaboutadogvol2”. It’s not perfect – “Story About A Dog, A” would be wrongly separated, for example – but close enough. Most of the books had ISBNs anyway, so the title was only a last resort.

Edit: Actually, looking at my code above, it looks like I DIDN’T do that but should’ve.

---

<div class="post-metadata">

**Author:** ![imageat](https://avatars.discourse-cdn.com/v4/letter/i/f17d59/32.png) [@imageat](https://boards.straightdope.com/u/imageat)\
**Post date:** [June 4, 2014, 9:52am UTC](https://boards.straightdope.com/t/making-a-6-digit-numeric-hash-from-a-string/637401/27 "2014-06-04T09:52:32Z")

</div>

thanks for sharing

---

<div class="post-metadata">

**Author:** ![derek054](https://avatars.discourse-cdn.com/v4/letter/d/b19c9b/32.png) [@derek054](https://boards.straightdope.com/u/derek054)\
**Post date:** [June 9, 2014, 7:55am UTC](https://boards.straightdope.com/t/making-a-6-digit-numeric-hash-from-a-string/637401/28 "2014-06-09T07:55:51Z")

</div>

In your case, a simply linear barcode will do, such as ISBN and EAN-13, etc, which all supports encoding data with a specified data length. Just google online and you will plenty of [barcode software](http://www.businessrefinery.com/) to do that.

---

<div class="post-metadata">

**Author:** ![Reply](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/reply/32/15952_2.png) [@Reply](https://boards.straightdope.com/u/Reply)\
**Post date:** [June 9, 2014, 8:12am UTC](https://boards.straightdope.com/t/making-a-6-digit-numeric-hash-from-a-string/637401/29 "2014-06-09T08:12:27Z")

</div>

> [@derek054](#):
>
> In your case, a simply linear barcode will do, such as ISBN and EAN-13, etc, which all supports encoding data with a specified data length. Just google online and you will plenty of [barcode software](http://www.businessrefinery.com/) to do that.

That’s exactly what we did. Hashed titles into ISBN-compatible barcodes.

[Previous page](https://boards.straightdope.com/t/making-a-6-digit-numeric-hash-from-a-string/637401.md?page=1)
