如何在Javascript中将Guid转换为Byte数组? [英] How can I convert a Guid to a Byte array in Javascript?

查看:165
本文介绍了如何在Javascript中将Guid转换为Byte数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a service bus, and the only way to transform data is via JavaScript. I need to convert a Guid to a byte array so I can then convert it to Ascii85 and shrink it into a 20 character string for the receiving customer endpoint.

任何想法都会受到赞赏.

Any thoughts would be appreciated.

推荐答案

尝试一下(需要大量测试):

Try this (needs LOTS of tests):

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 + "~>";
}

这篇关于如何在Javascript中将Guid转换为Byte数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆