UUID into unsigned int

醉酒当歌 提交于 2019-12-10 21:28:14

问题


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


回答1:


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.




回答2:


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.

xoring 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.




回答3:


Perhaps you could use a hash function to transform the UUID to always positive number and store the number into the unsigned int?



来源:https://stackoverflow.com/questions/7828702/uuid-into-unsigned-int

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!