关于编程生成UUID字符串的说明

一、在C#中生成UUID字符串:

// 创建GUID对象,通过GUID获取UUID
Guid guid = Guid.NewGuid();
// 返回UUID字符串
return guid.ToString();

二、在Java中生成UUID字符串:

// 创建UUID对象
UUID uuid = UUID.randomUUID();
// 返回UUID字符串
return uuid.toString();

三、在PHP中生成UUID字符串:

// 生成唯一标识字符串
$ids = strtolower ( md5 ( uniqid ( mt_rand (), true ) ) );
// 定义分隔符“-”
$hyphen = chr ( 45 );
// 构造UUID字符串
$uuid = substr ( $ids, 0, 8 );
$uuid .= $hyphen . substr ( $ids, 8, 4 );
$uuid .= $hyphen . substr ( $ids, 12, 4 );
$uuid .= $hyphen . substr ( $ids, 16, 4 );
$uuid .= $hyphen . substr ( $ids, 20, 12 );
// 返回UUID字符串
return $uuid;

四、在JavaScript中生成UUID字符串:

// 定义字符数组
var ids = [];
// 生成唯一标识字符数组
var hexDigits = "0123456789abcdef";
for (var i = 0; i < 36; i++) {
    ids[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
}
ids[14] = "4";
ids[19] = hexDigits.substr((ids[19] & 0x3) | 0x8, 1);
// 定义分隔符“-”
ids[8] = ids[13] = ids[18] = ids[23] = "-";
// 构造UUID字符串
var uuid = ids.join("");
// 返回UUID字符串
return uuid;

更多信息请参阅:UUID_百度百科

发表回复