admin管理员组

文章数量:1379603

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.

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.

Share Improve this question asked Jan 13, 2010 at 17:15 Chris KookenChris Kooken 34k15 gold badges92 silver badges129 bronze badges 3
  • Why does it have to be in JavaScript? There is no native byte value. – ChaosPandion Commented Jan 13, 2010 at 17:19
  • Whatever you e up with will be hacky and awful in JavaScript, and scarcely worthy of an SO answer. A better question might be: "How do I improve my service architecture so I can transform data more robustly?" – iandisme Commented Jan 13, 2010 at 17:24
  • The only scripting language the service bus supports is JavaScript. It is for sending Patient information VIA HL7 the product name is MIRTH and it is open source. – Chris Kooken Commented Jan 14, 2010 at 0:28
Add a ment  | 

3 Answers 3

Reset to default 2

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

This is a VERY old question but it shows up top of google search results so here is the new age correct answer.

Buffer.from(guid.replaceAll('-',''), 'hex')

Check the unparse() method in node-uuid package and its example here:

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

本文标签: stringHow can I convert a Guid to a Byte array in JavascriptStack Overflow