Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store UUID as int

I m having some issues here.. I am using the following code to generate the uuid in mine application.

    - (NSString *)GetUUID
{
    CFUUIDRef theUUID = CFUUIDCreate(NULL);
    CFStringRef string = CFUUIDCreateString(NULL, theUUID);
    CFRelease(theUUID);
    return [(NSString *)string autorelease];
}

This code returns a NSString object back. Now I want to store the generated UUID as unsigned int. Any suggestions please how can i do it here. Thanks.

like image 213
Hadi Avatar asked Sep 21 '11 04:09

Hadi


People also ask

Should I use UUID or INT?

bigserial advantages: Integer sequences occupy only 64 bits while UUIDs are 128, so there's a slight storage saving. There's more space in a UUID, but 64 bits is plenty big enough to never run out of IDs. Integers are more human friendly.

Is UUID stored as string?

A UUID – that's short for Universally Unique IDentifier, by the way – is a 36-character alphanumeric string that can be used to identify information (such as a table row).

What is the datatype for UUID in MySQL?

In MySQL, a UUID value is a 128-bit number represented as a utf8 string, and the format in hexadecimal number will be as follows. Here, the first three numbers are generated from the low, middle, and high parts of a timestamp.

How are UUID stored in database?

In MySQL, you can store UUID values in a compact format ( BINARY ) and display them in human-readable format ( VARCHAR ) with help of the following functions: UUID_TO_BIN.


2 Answers

According to the Wikipedia page, UUIDs are 128 bits long. The largest int value you'll be able to work with is 64 bits. Therefore, I'd says you can't store it in an int because there simply isn't room.

like image 134
LandonSchropp Avatar answered Oct 28 '22 16:10

LandonSchropp


Convert UUID to String using .UUIDString

Take string's .hash property.

Cast to int.

int numericFormUUID = (int)UUIDString.hash;


Note: collision is possible, as always, with hashing. Two users could end up with the same value here, whereas UUID is guaranteed unique.

like image 45
Albert Renshaw Avatar answered Oct 28 '22 17:10

Albert Renshaw