linux c 產生uuid

來源:互聯網
上載者:User

標籤:blog   io   ar   for   div   sp   on   c   log   

#include <stdio.h>#include <stdlib.h>#include <string.h>/** * Create random UUID * * @param buf - buffer to be filled with the uuid string */char *random_uuid( char buf[37] ){const char *c = "89ab";    char *p = buf;    int n;    for( n = 0; n < 16; ++n )    {        int b = rand()%255;        switch( n )        {            case 6:                sprintf(p, "4%x", b%15 );break;            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);printf("guid = %s\n", guid);random_uuid(guid);printf("guid = %s\n", guid);random_uuid(guid);printf("guid = %s\n", guid);random_uuid(guid);printf("guid = %s\n", guid);}

  運行如下:

[email protected]:~/src$ gcc getuuid.c[email protected]:~/src$ ./a.out guid = a397a255-53be-41fc-996b-521413e9e22dguid = 518e1f56-0857-49a7-a5d0-528277751b99guid = 4aed583d-6a52-49d5-b668-8ead955f3c35guid = b5c48cdd-6c11-453d-91b4-59cfce233d27[email protected]:~/src$ 

  

linux c 產生uuid

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.