How to convert guid to byte array in javascript?

I have a service bus, and the only way to convert data is through JavaScript. I need to convert Guid to an array of bytes, then convert it to Ascii85 and compress it to a string with 20 characters for the endpoint of the receiving client.

Any thoughts would be appreciated.

+1
source share
2 answers

Try it (many tests required):

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
source

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

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

0
source

Source: https://habr.com/ru/post/947625/


All Articles