我从 WebSocket 连接获取 arrayBuffur,并且我可以获得字节数组的范围,即 用 C# 创建的指南。
如何将此 guid 字节转换为 JavaScript 中的字符串?
Guid="FEF38A56-67A9-46DF-B7D8-C52191CD70F4"
字节=[86, 138, 243, 254, 169, 103, 223, 70, 183, 216, 197, 33, 145, 205, 112, 244]
谢谢。
最佳答案
除了 JohanShogun 的回答之外,还有一个简单的脚本可以在字节数组上使用映射函数。但是,根据此备注部分: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
关于javascript - 在javascript中将guid字节转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17946399/