/********方法一**********/
#include <stdio.h>
#include <stdlib.h>
#include <string.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;
} void main()
{
char guid[37];
random_uuid(guid);
printf("guid = %s\n", guid);
random_uuid(guid);
printf("guid = %s\n", guid);
random_uuid(guid);
printf("guid = %s\n", guid);
random_uuid(guid);
printf("guid = %s\n", guid);
}

  运行如下:

daniel@debian:~/src$ gcc getuuid.c
daniel@debian:~/src$ ./a.out
guid = a397a255-53be-41fc-996b-521413e9e22d
guid = 518e1f56-0857-49a7-a5d0-528277751b99
guid = 4aed583d-6a52-49d5-b668-8ead955f3c35
guid = b5c48cdd-6c11-453d-91b4-59cfce233d27
daniel@debian:~/src$

 

/********方法二**********/
/*libuuid是一个用于生成UUID的C库,具体用法参考http://linux.die.net/man/3/libuuid,示例如下:*/ #include <stdio.h>
#include <uuid/uuid.h> int main(int argc, char **argv)
{
uuid_t uuid;
char str[36]; uuid_generate(uuid);
uuid_unparse(uuid, str); printf("%s\n", str); return 0;
}
在Linux下编译时需要链接uuid库 gcc -o uuid uuid.c -luuid
在Ubuntu中,可以用下面的命令安装libuuid: sudo apt-get install uuid-dev

linux c 生成uuid的更多相关文章

  1. php生成UUID

    UUID含义是 通用唯一识别码 (Universally Unique Identifier),这 是一个软件建构的标准,也是被开源软件基金会 (Open Software Foundation, O ...

  2. java生成UUID通用唯一识别码 (Universally Unique Identifier)

    转自:http://blog.csdn.net/carefree31441/article/details/3998553 UUID含义是通用唯一识别码 (Universally Unique Ide ...

  3. (转)java生成UUID通用唯一识别码 (Universally Unique Identifier)

    (原文链接:http://blog.csdn.net/carefree31441/article/details/3998553)   UUID含义是通用唯一识别码 (Universally Uniq ...

  4. java生成UUID通用唯一识别码 (Universally Unique Identifier) 分类: B1_JAVA 2014-08-22 16:09 331人阅读 评论(0) 收藏

    转自:http://blog.csdn.net/carefree31441/article/details/3998553 UUID含义是通用唯一识别码 (Universally Unique Ide ...

  5. Java 生成 UUID

    1.UUID 简介 UUID含义是通用唯一识别码 (Universally Unique Identifier),这是一个软件建构的标准,也是被开源软件基金会 (Open Software Found ...

  6. linux下生成core dump文件方法及设置

    linux下生成core dump文件方法及设置    from:http://www.cppblog.com/kongque/archive/2011/03/07/141262.html core ...

  7. JS生成UUID的方法实例

    <!DOCTYPE html> <html> <head> <script src="http://libs.baidu.com/jquery/1. ...

  8. linux下安装uuid库

    1.linux 下安装UUID库 1.1)ubuntu下安装uuid链接库 sudo apt-get install uuid-dev 1.2)CentOS yum install libuuid-d ...

  9. java 生成UUID

    UUID(Universally Unique Identifier)全局唯一标识符,是一个128位长的数字,一般用16进制表示. 算法的核心思想是结合机器的网卡.当地时间.一个随即数来生成UUID, ...

随机推荐

  1. Geo-Fence

    转自:http://blog.jobbole.com/86633/ 地理围栏(Geo-fencing)是LBS的一种应用,就是用一个虚拟的栅栏围出一个虚拟地理边界,当手机进入.离开某个特定地理区域,或 ...

  2. HDU 5564 Clarke and digits 状压dp+矩阵加速

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5564 题意: 求长度在[L,R]范围,并且能整除7的整数的总数. 题解: 考虑最原始的想法: dp[ ...

  3. Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor 模拟

    题目链接: http://codeforces.com/contest/670/problem/E 题解: 用STL的list和stack模拟的,没想到跑的还挺快. 代码: #include<i ...

  4. 控制UIlabel 垂直方向对齐方式的 方法

    最正统的方法,利用objective-c的category特性,修改UILabel的绘制代码.示例代码如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ...

  5. [工作积累] bitfield

    ISO/IEC 14882:2003: 9.6 Bit-fields [class.bit] A member-declarator of the form identifieropt : const ...

  6. HackPorts – Mac OS X 渗透测试框架与工具

    HackPorts是一个OS X 下的一个渗透框架. HackPorts是一个“超级工程”,充分利用现有的代码移植工作,安全专业人员现在可以使用数以百计的渗透工具在Mac系统中,而不需要虚拟机. 工具 ...

  7. LINUX下的时间与时区的设置

    在RHEL下,如果只装英文版系统,设置好时区以后(上海时间,UTC) 在命令行下用date命令查看,总是与实际的北京时间差8小时,其实硬件时间都是准确的.会带来视觉不便. 今天下决心解决此问题,不过也 ...

  8. Loadrunner监控Centos

    一.安装必要包 yum istall gcc gcc-c++ rpcbind -y 二.下载安装必要软件rstatd 下载并安装rstatd,下载地址:http://sourceforge.net/p ...

  9. HDU 1385 Minimum Transport Cost (Dijstra 最短路)

    Minimum Transport Cost http://acm.hdu.edu.cn/showproblem.php?pid=1385 Problem Description These are ...

  10. 自制 JS.format带分页索引

    //第一参数是:Json对象,第二个是   序号  第三个   页数     第四  当前页数String.prototype.format = function (args, sid, pagesi ...