2

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.

4

2 回答 2

4

除了 JohanShogun 的回答之外,一个简单的脚本可以在字节数组上使用 map 函数。但是,根据此注释部分:http: //msdn.microsoft.com/pl-pl/library/system.guid.tobytearray.aspx,前四个字节应该颠倒,以及接下来的两个和后面的两个。所以......这是正确的解决方案(不要忘记结果值 15 的零填充应该是“0F”而不是“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

此处测试示例:http: //jsbin.com/ogegut/4/edit

于 2013-07-30T12:32:06.273 回答
0

如果您将这些用作数字(http://www.w3schools.com/jsref/jsref_obj_number.asp),您只需执行 .toString (16) 即可将它们设为十六进制。

于 2013-07-30T12:09:11.610 回答