Is there anywhere to compress/convert/encode/encrypt a UUID into an unsigned int?
I read UUID's from a sql table, the history is ugly and i can not change... I have only an unsigned int to store it in. This is C++ in case that makes a difference
Any thoughts on this?
Thanks Reza
As others have said, you will lose information translating a 128-bit UUID to a narrower integer type.
If you want to guarantee uniqueness -- well, that's what UUIDs are for, after all, and you might just consider keeping the information in UUID format.
If you can settle for a low chance of collisions (two distinct UUIDs mapping to the same integer), there are several things you can try.
Use as big an integer type as you can. If your compiler supports an unsigned 64-bit integer type (unsigned long long
or whatever Microsoft calls it), use that.
xor
ing the upper and lower 64-bit halves of the UUID should give you a reasonably decent hash.
If there's some kind of order (non-randomness, predictability) in the UUID values that makes that unsuitable, you can compute an md5 or sha-1 hash and discard all but 64 bits. It doesn't matter which bits you discard.
If you're restricted to a 32-bit integer, you can xor
the four 32-bit quarters of the UUID together, or discard all but 32 bits of the md5 or sha-1 hash.
Note that in the case of 32-bit integers, you could conceivably have a collision on your first two samples, but that's unlikely. The likelihood of a collision rises to roughly 50% with a number of samples somewhere around the square root of the total number of possibilities, so if you have 100,000 random 32-bit numbers, it's likely that two of them will be the same. See the Birthday Paradox.
Use a CRC32 of the UUID (I assume you mean 32-bit integer). There's obviously the potential for collisions, but if there's a collision it should be rare enough you can just fix it manually.
Note that if this is MS SQL Server, you can use the CHECKSUM
function to do a crc32 hash on the server to update your table.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With