ArrayBuffer 转换为字符串
2021-02-26 本文已影响0人
牛年大吉2021
编写蓝牙小程序时,读取的数据为ArrayBuffer,需要转换为字符串,转换如下:
ab2str: function(buf) {
return String.fromCharCode.apply(null, new Uint8Array(buf));
},
// ArrayBuffer转16进度字符串示例
function ab2hex(buffer) {
const hexArr = Array.prototype.map.call(
new Uint8Array(buffer),
function (bit) {
return ('00' + bit.toString(16)).slice(-2)
}
)
return hexArr.join('')
}