如何在Javascript中将Guid转换为Byte数组?

时间:2010-01-13 17:15:14

标签: javascript string guid ascii

我有一个服务总线,转换数据的唯一方法是通过JavaScript。我需要将Guid转换为字节数组,然后我可以将其转换为Ascii85并将其缩小为接收客户端点的20个字符的字符串。

任何想法都会受到赞赏。

2 个答案:

答案 0 :(得分:1)

试试这个(需要很多测试):

var guid = "{12345678-90ab-cdef-fedc-ba0987654321}";
window.alert(guid + " = " + toAscii85(guid))

function toAscii85(guid)
{
    var ascii85  = ""
    var chars    = guid.replace(/\{?(?:(\w+)-?)\}?/g, "$1");
    var patterns = ["$4$3$2$1", "$2$1$4$3", "$1$2$3$4", "$1$2$3$4"];
    for(var i=0; i < 32; i+=8)
    {
        var block = chars.substr(i, 8)
            .replace(/(..)(..)(..)(..)/, patterns[i / 8]) //poorman shift
        var decValue = parseInt(block, 16);

        var segment = ""
        if(decValue == 0)
        {
            segment = "z"
        }
        else
        {
            for(var n = 4; n >= 0; n--)
            {
                segment = String.fromCharCode((decValue % 85) + 33) + segment;
                decValue /= 85;
            }
        }
        ascii85 += segment
    }
    return "<~" + ascii85 + "~>";
}

答案 1 :(得分:0)

检查node-uuid包中的 unparse()方法及其示例:

https://www.npmjs.com/package/node-uuid#uuid-unparse-buffer-offset