大家好,如題,不使用uuid庫,如何實作一個生成uuid函式? 我從網上找了一個,貌似有問題,代碼如下:
/**
* Create random UUID
*
* @param buf - buffer to be filled with the uuid string
*/
static 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 Test()
{
char buf[37] = { 0 };
random_uuid(buf);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/194483.html
標籤:應用程序開發區
