How can I convert a Guid to a Byte array in Javascript?
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 the开发者_如何学JAVAn 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 + "~>";
}
Check the unparse() method in node-uuid package and its example here:
https://www.npmjs.com/package/node-uuid#uuid-unparse-buffer-offset
精彩评论