全局唯一标识符 (GUID)

Posted sntetwt

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了全局唯一标识符 (GUID)相关的知识,希望对你有一定的参考价值。

javascript生成GUID

算法一:3b96b619-0a23-4ca1-80d4-8d4838173431

function uuid() {
    var s = [];
    var hexDigits = "0123456789abcdef";
    for (var i = 0; i < 36; i++) {
        s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
    }
    s[14] = "4";
    s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);
    s[8] = s[13] = s[18] = s[23] = "-";

    var uuid = s.join("");
    return uuid;
}

 算法二:1704354b-6274-4256-a2e1-b2710672e57c

function guid() {
    return ‘xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx‘.replace(/[xy]/g, function (c) {
        var r = Math.random() * 16 | 0, v = c == ‘x‘ ? r : (r & 0x3 | 0x8);
        return v.toString(16);
    });
}

 算法三:17bd3f96-513b-c4e4-1b37-936b2031d00c

function guid() {
    function S4() {
        return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
    }
    return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
}

 算法四:
二进制:guid(16,2);1111010101100011
十进制:guid(16,10);7459326145909010
十六进制:guid(16,16);42DAD4B14F6615CF

function guid(len, radix) {
    var chars = ‘0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz‘.split(‘‘);
    var uuid = [], i;
    radix = radix || chars.length;
    if (len) {
        for (i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix];
    } else {
        var r;
        uuid[8] = uuid[13] = uuid[18] = uuid[23] = ‘-‘;
        uuid[14] = ‘4‘;
        for (i = 0; i < 36; i++) {
            if (!uuid[i]) {
                r = 0 | Math.random() * 16;
                uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
            }
        }
    }
    return uuid.join(‘‘);
}

 C#生成GUID的四种格式

格式一:9af7f46a-ea52-4aa3-b8c3-9fd484c2af12

var uuid = Guid.NewGuid().ToString(); 

 
格式二:e0a953c3ee6040eaa9fae2b667060e09

var uuidN = Guid.NewGuid().ToString("N");

 
格式三:9af7f46a-ea52-4aa3-b8c3-9fd484c2af12

var uuidD = Guid.NewGuid().ToString("D");

 
格式四:{734fd453-a4f8-4c5d-9c98-3fe2d7079760}

var uuidB = Guid.NewGuid().ToString("B");

 
格式五:(ade24d16-db0f-40af-8794-1e08e2040df3)

var uuidP = Guid.NewGuid().ToString("P"); 

 
格式六:{0x3fa412e3,0x8356,0x428f,{0xaa,0x34,0xb7,0x40,0xda,0xaf,0x45,0x6f}}

var uuidX = Guid.NewGuid().ToString("X");

 

以上是关于全局唯一标识符 (GUID)的主要内容,如果未能解决你的问题,请参考以下文章

GUID (全局唯一标识符)

GUID 全局唯一标识符

创建全局唯一标识,guid

创建全局唯一标识,guid

全局唯一标识符 (GUID)

GUID