前言:
承接上篇文章,由於藍牙設備返回值時進制形式的byte,所以要用到本篇文章進行解析
代碼:
//byte數組轉換為無符號short整數
byteToUnsignedShort(bytes, off) {
let high = bytes[off + 1];
let low = bytes[off];
return (high << 8 & 0xFF00) | (low & 0xFF);
},
//無符號short轉換為2字節的byte數組
unsignedShortToByte(s){
let targets = [];
targets[1] = (s >> 8 & 0xFF);
targets[0] = (s & 0xFF);
return targets;
},
//字符串轉換為2字節的byte數組
stringToByte(array){
let hexArray = []
for(let i=0;i<array.length;i++){
let value = parseInt(array[i], 16)
hexArray.push(value)
}
return hexArray;
},
參考文案:
https://segmentfault.com/a/1190000018994576
