Question

I have the following code for printing a UUID which works fine:

void puid(uuid_t u)
{
    int i;

    printf("%8.8x-%4.4x-%4.4x-%2.2x%2.2x-", u.time_low, u.time_mid,
    u.time_hi_and_version, u.clock_seq_hi_and_reserved,
    u.clock_seq_low);
    for (i = 0; i < 6; i++)
        printf("%2.2x", u.node[i]);
    printf("\n");
}

Example output:

22b31d0d-4814-56e9-ba30-6c23d328deaf

How would I go about constructing a char string to save the above output in?

Was it helpful?

Solution

what about :

char uuid[40];

sprintf(uuid, "%8.8x-%4.4x-%4.4x-%2.2x%2.2x-%2.2x%2.2x%2.2x%2.2x%2.2x%2.2x", 
u.time_low, u.time_mid, u.time_hi_and_version, u.clock_seq_hi_and_reserved,
u.clock_seq_low, u.node[0], u.node[1], u.node[2], u.node[3], u.node[4], u.node[5]);

printf("%s\n", uuid);

OTHER TIPS

Use sprintf()

int sprintf ( char * str, const char * format, ... );

Write formatted data to string

Composes a string with the same text that would be printed if format was used on printf, but instead of being printed, the content is stored as a C string in the buffer pointed by str.

UUID is 36 characters length always. so declare a char array of 37 chars.

and use sprintf() for forming string.

See definition section.

http://en.wikipedia.org/wiki/Universally_unique_identifier

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top