Home > OS >  Do not use the uuid library, how to implement a uuid generated function?
Do not use the uuid library, how to implement a uuid generated function?

Time:10-28

Everybody is good, such as title, do not use the uuid library, how to implement a uuid generated function? I'm from the Internet to find a new one, seems to have a problem, the code is as follows:

 
/* *
* Create random UUID
*
* @ param buf - buffer to be filled with the uuid string
*/

The static char * random_uuid (char buf [37])
{
Const char * c="89 ab";
Char * p=buf.
int n;
For (n=0; N & lt; 16. + + n)
{
Int b=rand () % 255;
The switch (n)
{
Case 6:
Sprintf (p, "x" 4%, b % 15);
break;
Case 8:
Sprintf (p, "% % x c, c [rand () % strlen (c)], b % 15);
break;
Default:
Sprintf (p, "% 02 x", b);
break;
}

P +=2;
The switch (n)
{
Case 3:
Case 5:
Case 7:
Case 9:
* p++='-';
break;
}
}
* p=0;
Return buf.
}

//function call
Void Test ()
{
Char buf [37]={0};
Random_uuid (buf);
}

  • Related