Linux C build UUID

Source: Internet
Author: User
Tags rand sprintf

#include <stdio.h> #include <stdlib.h> #include <string.h>/** * Create Random UUID * * @param buf-buffer    To is filled with the UUID string */char *random_uuid (char buf[37]) {const char *c = "89ab";    char *p = BUF;    int n;        for (n = 0; n < ++n) {int b = rand ()%255;            Switch (n) {case 6:sprintf (p, "4%x", b%15);            Case 8:sprintf (P, "%c%x", C[rand ()%strlen (c)], b%15);            Break            default:sprintf (P, "%02x", b);        Break        } p + = 2; Switch (n) {case 3:case 5:case 7:case 9: *p++ = '-'                ;        Break    }} *p = 0; return BUF;} void Main () {char guid[37];random_uuid (GUID);p rintf ("GUID =%s\n", GUID), Random_uuid (GUID);p rintf ("GUID =%s\n", GUID); Random_uuid (GUID);p rintf ("GUID =%s\n", GUID), Random_uuid (GUID);p rintf ("GUID =%s\n",GUID);} 

Run as follows:

  

Linux C build UUID

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.