Skip to content

Instantly share code, notes, and snippets.

@yoggy
Created January 8, 2013 11:19
Show Gist options
  • Star 40 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save yoggy/4483031 to your computer and use it in GitHub Desktop.
Save yoggy/4483031 to your computer and use it in GitHub Desktop.
libuuid sample program
//
// libuuid sample program
//
// library install for debian
// $ sudo apt-get install uuid-dev
//
// compile
// $ gcc uuid_test.c -luuid -o uuid_test
//
#include <stdio.h>
#include <uuid/uuid.h>
int main(int argc, char *argv[])
{
// typedef unsigned char uuid_t[16];
uuid_t uuid;
// generate
uuid_generate_time_safe(uuid);
// unparse (to string)
char uuid_str[37]; // ex. "1b4e28ba-2fa1-11d2-883f-0016d3cca427" + "\0"
uuid_unparse_lower(uuid, uuid_str);
printf("generate uuid=%s\n", uuid_str);
// parse (from string)
uuid_t uuid2;
uuid_parse(uuid_str, uuid2);
// compare (rv == 0)
int rv;
rv = uuid_compare(uuid, uuid2);
printf("uuid_compare() result=%d\n", rv);
// compare (rv == 1)
uuid_t uuid3;
uuid_parse("1b4e28ba-2fa1-11d2-883f-0016d3cca427", uuid3);
rv = uuid_compare(uuid, uuid3);
printf("uuid_compare() result=%d\n", rv);
// is null? (rv == 0)
rv = uuid_is_null(uuid);
printf("uuid_null() result=%d\n", rv);
// is null? (rv == 1)
uuid_clear(uuid);
rv = uuid_is_null(uuid);
printf("uuid_null() result=%d\n", rv);
return 0;
}
@mortymacs
Copy link

Thanks a lot! 👍

@celan69
Copy link

celan69 commented Oct 7, 2022

Thank you! Just what I needed...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment