Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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;
}
@eduardonunesp
Copy link

eduardonunesp commented Sep 27, 2014

Thanks, very nice :D

@edo9k
Copy link

edo9k commented Jul 6, 2018

Thanks! This is a useful little snippet.

@mortymacs
Copy link

mortymacs commented Jul 19, 2020

Thanks a lot! 👍

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