zl程序教程

您现在的位置是:首页 >  其他

当前栏目

c语言生成随机uuid编码示例

编码语言 示例 生成 随机 UUID
2023-06-13 09:15:26 时间

c语言生成随机uuid编码

复制代码代码如下:


#include<stdio.h>
#include<stdlib.h>

/**
 *CreaterandomUUID
 *
 *@parambuf-buffertobefilledwiththeuuidstring
 */
char*random_uuid(charbuf[37])
{
   constchar*c="89ab";
   char*p=buf;
   intn;

   for(n=0;n<16;++n)
   {
       intb=rand()%255;

       switch(n)
       {
           case6:
               sprintf(
                   p,
                   "4%x",
                   b%15);
               break;
           case8:
               sprintf(
                   p,
                   "%c%x",
                   c[rand()%strlen(c)],
                   b%15);
               break;
           default:
               sprintf(
                   p,
                   "%02x",
                   b);
               break;
       }

       p+=2;

       switch(n)
       {
           case3:
           case5:
           case7:
           case9:
               *p++="-";
               break;
       }
   }

   *p=0;

   returnbuf;
}