Sign in to follow this  
impulsionaudio

32-bit GUID

Recommended Posts

impulsionaudio    136
I would like to create a 32-bit GUID to use as a key for associative containers. I say GUID because I don't have any identifying data (e.g. name) to feed to a hash function to generate a hash value for the key. Windows CoCreateGUID function creates a 128-bit GUID which is not what I want. Any ideas? Thanks.

Share this post


Link to post
Share on other sites
prh99    520
You can take the unsigned int or the shorts of out of the GUID struct that's passed to CoCreateGUID().


typedef struct _GUID {
unsigned long Data1;
unsigned short Data2;
unsigned short Data3;
unsigned char Data4[8];
} GUID,


You could also just pick random integer to start with and just increment it for each addition to the container.

Hope that helps.

Share this post


Link to post
Share on other sites
LessBread    1415
GUID's are 128 bit. 32 bits and they're just ID's. prh99's suggestion sounds suitable. One call to CoCreateGUID and a cast to DWORD array[4] you've got 4, 32 bit id's.

Share this post


Link to post
Share on other sites
Well, GUIDs are not unique anyway, they're very likely unique in the foreseeable future.

But the real question is: do you actual need the properties of GUIDs? The quote "as a key for associative containers" suggests to me that something like crc32(namestring) is maybe "unique enough". You probably don't own nearly enough keys to cause a collision in crc32.

Or, if you just want to give a distinct number to any object you read in from a file, so you can refer to it later, then something like: int id() {static int i; return ++i; } might just do.

You will only want the "GU" part if you absolutely need to make sure that an object you create on your PC today can be distinguished both from one you generate the next millisecond or in a year, and from one you generate on another PC at the same time. Most applications really don't need this.

Share this post


Link to post
Share on other sites
iMalc    2466
It all depends on where the IDs come from. If they're all generated from the one process on the one machine, then it's not really GUIDs you want, but just UIDs, in which case a cookie generator will do just fine. If they can come from anywhere then only a real 128bit GUID will help.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this