Question

I am getting arrayBuffur from WebSocket connection, and I can get range of byte arrays that is Guid created in c#.

How I can convert this guid bytes to string in javascript?

Guid="FEF38A56-67A9-46DF-B7D8-C52191CD70F4"

Bytes=[86, 138, 243, 254, 169, 103, 223, 70, 183, 216, 197, 33, 145, 205, 112, 244]

Thanks.

Was it helpful?

Solution

In addition to JohanShogun answer, a simple script to do that would utilize map function on an array of bytes. However, according to Remarks section of this: http://msdn.microsoft.com/pl-pl/library/system.guid.tobytearray.aspx , first four bytes should be reversed, as well as two next and two after them. So... here's correct solution (don't forget about zero-padding of resulting values 15 should be "0F" not "F"):

var x = [168, 199, 56, 91, 146, 52, 231, 64, 175, 133, 167, 15, 146, 60, 83, 107];

// reverse first four bytes, and join with following two reversed, joined with following two reversed, joined with rest of the bytes
x = x.slice(0, 4).reverse().concat(x.slice(4,6).reverse()).concat(x.slice(6,8).reverse()).concat(x.slice(8))

var guid = x.map(function(item) {
    // return hex value with "0" padding
    return ('00'+item.toString(16).toUpperCase()).substr(-2,2);
})

// guid variable now holds string: 5B38C7A8349240E7AF85A70F923C536B

Testing example here: http://jsbin.com/ogegut/4/edit

OTHER TIPS

If you're using these as Numbers (http://www.w3schools.com/jsref/jsref_obj_number.asp) you can simply do .toString (16) to get them in hex.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top