欢迎来到工商注册核名查询系统!

C语言

当前位置:主页 > 软件编程 > C语言 >

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

来源:本站原创|时间:2022-11-25|栏目:C语言|

c语言生成随机uuid编码

复制代码 代码如下:

#include <stdio.h>
#include <stdlib.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;
}

更多C语言

您可能感兴趣的文章

阅读排行

本栏相关

随机阅读

网页制作CMS教程网络编程软件编程脚本语言数据库服务器

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:835971066 | 邮箱:835971066#qq.com(#换成@)

Copyright © 2002-2020 工商注册核名查询系统 版权所有