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

这篇文章主要介绍了c语言生成随机uuid编码示例,需要的朋友可以参考下

c语言生成随机uuid编码

复制代码 代码如下:

#include
#include

/**
 * 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语言生成随机uuid编码示例的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » C语言