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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

  2021-03-19 我要评论
想了解c语言生成随机uuid编码示例的相关内容吗,在本文为您仔细讲解c语言生成uuid的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:c语言,uuid编码,下面大家一起来学习吧。

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;
}

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们