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;
}
?>
本文对比了在Cocos2dx环境下使用UUID.h头文件生成UUID的方法与PHP中利用microtime函数生成UUID的过程,包括代码实现及原理解析。
2203

被折叠的 条评论
为什么被折叠?



