js byte 与 进制 相互转换
前言:
承接上篇文章,由于蓝牙设备返回值时进制形式的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