获取唯一设备标识符

本文对比了在Cocos2dx环境下使用UUID.h头文件生成UUID的方法与PHP中利用microtime函数生成UUID的过程,包括代码实现及原理解析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Cocos2dx下,包含头文件<uuid/uuid.h>

std::string uid = "";
    uuid_t uu;
    int i;
    uuid_generate(uu);
    for (i = 0; i < 16; i ++) {
        char aa[10]="";
        sprintf(aa, "%02X",uu[i]);
        uid+=aa;
    }
    return uid;

php下(转)

<?php
	function create_guid(){
		$micortime = microtime();
		list($a_dec,$a_sec) = explode(" ", $micortime);
		$dec_hex = dechex($a_dec*1000000);
		$sec_hex = dechex($a_sec);
		ensure_length($dec_hex, 5);
		ensure_length($sec_hex, 6);
		$guid = "";
		$guid.=$dec_hex;
		$guid.=create_guid_section(3);
		$guid.='-';
		$guid.=create_guid_section(4);
		$guid.='-';
		$guid.=create_guid_section(4);
		$guid.='-';
		$guid.=create_guid_section(4);
		$guid.='-';
		$guid.=$sec_hex;
		$guid.=create_guid_section(6);
		return $guid;
	}
	
	function ensure_length(&$string,$length){
		$strlen = strlen($string);
		if ($strlen<$length) {
			$string = str_pad($string, $length,"0");
		}
		elseif ($strlen>$length){
			$string = substr($string, 0,$length);
		}
	}
	
	function create_guid_section($characters){
		$return = "";
		for ($i = 0;$i < $characters;$i++){
			$return.=dechex(mt_rand(0, 15));
		}
		return $return;
	}
	
?>



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值