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

Jessica ·
更新时间:2024-09-20
· 765 次阅读

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语言/C++中如何产生随机数c语言中 基于随机函数的使用详解c语言生成随机数的方法(获得一组不同的随机数)C语言中用于产生随机数的函数使用方法总结C语言/C++如何生成随机数C语言实现的排列组合问题的通用算法、解决方法C语言实现输入一个字符串后打印出该字符串中字符的所有排列使用C语言解决字符串全排列问题排列和组合算法的实现方法_C语言经典案例C语言实现文件内容按行随机排列的算法示例



示例 uuid C语言

需要 登录 后方可回复, 如果你还没有账号请 注册新账号