Ask HN: Why not use a higher base than Hex for UUIDs if they're just strings?
With Hexadecimal, we get 4 bits per character. With Base64, we get 6 bits per character. To keep UUIDs at 128 bits, we'd go from 32 to 22 characters in length.
So why do we use hexadecimal representation when it is literally just a string, and could be anything? Even keeping the radix in powers of 2 is kind of unnecessary if these are being stored and transmitted as strings, the radix at that point just becomes a mechanism for allowing additional combinations. We could use any arbitrary number of characters per digit and it would serve that purpose. There are 91 ASCII characters available, so what gives?
Additionally, why the dashes? If we reserve digits for meaning per standard, such as UUIDv1 MAC address + time, why not just go off of digit position? In a string, that dash takes up just as much wasted space as the additional characters which are providing data utility.
7 comments
[ 4.6 ms ] story [ 24.6 ms ] threadIn practice, saving those few bytes will usually be just noise compared to the rest of the data (unless you're storing primarily uuids, but then you should've chosen a better solution)
We've been using a custom 12-character format where the first character is a type and the last 11 characters is a random int encoded in a base-62 alphabet (url safe, no punctuation, etc) -- this gives us ~17 billion billion combinations -- almost 64 bits.
IPFS is one of several projects adopting the scheme.
https://en.wikipedia.org/wiki/Base58
Instead of base64, sensible databases and libraries store UUIDs as "base256". Actual binary strings of 128 bits = 16 bytes, not printable ASCII. That is the shortest possible representation, with zero wasted space.
The hexadecimal representation is only used for displaying UUIDs to humans. It doesn't have to be efficient, it has to be helpful.
For UUID version 1, the dashes separate clock, counter and MAC address parts in the data. With hex encoding it is easy to convert to/from binary and for "popular numbers" even to decimal in your head without running it through a baseXX converter.
For UUID version 4, where the data is all completely random, it's less useful. All you can do with it is compare two UUIDs. So there perhaps the human-friendly representation could do without dashes and use a larger base.
They are really just a 128-bit number.