蓝牙数据应用示例

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>

    /**
     * hex 16进制
     * dex 10进制
    */

    // 10进制转16进制
    function dex2hex(dec, len = 4) {
      let hex = "";
        while( dec ) {
        let last = dec & 15;
        hex = String.fromCharCode(((last > 9) ? 55 : 48) + last) + hex;
        dec >>= 4;
        }
        if (len) {
          while(hex.length < len) hex = '0' + hex;
        }
        return hex;
    }
        
    // 16进制转10进制
    function hex2dex(str) {
      return parseInt(str, 16).toString(10)
    }

    // 16进制转ASCII码
    function hexCharCodeToStr(hexCharCodeStr) {
      let trimedStr = hexCharCodeStr.trim();
      let rawStr = trimedStr.substr(0, 2).toLowerCase() === "0x" ? trimedStr.substr(2) : trimedStr;
      let len = rawStr.length;
      if (len % 2 !== 0) {
        alert("存在非法字符!");
        return "";
      }
      let curCharCode = '';
      let resultStr = [];
      for (let i = 0; i < len; i = i + 2) {
        curCharCode = parseInt(rawStr.substr(i, 2), 16);
        resultStr.push(String.fromCharCode(curCharCode));
      }
      return resultStr.join("");
    }

    // ArrayBuffer转16进制字符串
    function ab2hex(buffer) {
      return Array.prototype.map.call(
        new Uint8Array(buffer),
        function (bit) {
          return ('00' + bit.toString(16)).slice(-2)
        }
      ).join('')
    }

    // 16进制字符串转为ArrayBuffer
    function str2ab(str) {
      let buf = new ArrayBuffer(str.length / 2);
      let bufView = new Uint8Array(buf);
      for (let i = 0, strLen = str.length; i < strLen; i++) {
        bufView[i] = parseInt(str.slice(i * 2, i * 2 + 2), 16);
      }
      return buf;
    }


    /* 示例一 */
    // 10进制数值(最初的值)
    const a0 = 5888888.0; console.log('a0', a0);
    // 10进制 [转] 16进制字符串
    const a1 = dex2hex(a0); console.log('a1', a1);
    // 16进制字符串 [转] ArrayBuffer
    const a2 = str2ab(a1); console.log('a2', a2);
    // ArrayBuffer [转] 16进制字符串
    const a3 = ab2hex(a2); console.log('a3', a3);
    // 16进制字符串 [转] 10进制(得到最初的值)
    const a4 = hex2dex(a3); console.log('a4', a4);

    /* 示例二 */
    // 16进制的字符串(由多个16进制值拼接的字符串)
    const b0 = '6810BBBBBBBBBBBBBB0514AABB00';
    // b0 转成 ArrayBuffer
    const b1 = str2ab(b0);
    // b1 转成 16进制字符串
    const b2 = ab2hex(b1);

    /* 示例三 */
    const test1 = [1, '1', 100, 25, 380, 10, 0, '00'];
    let hexStr = ''; // 16进制的字符串
    test1.forEach(dex => {
      // 10进制转16进制
      hexStr = hexStr + dex2hex(dex, 4); // 2hex
    });
    console.log(hexStr); // 0001000100640019017C000A0000

    const test2 = [];
    for (let i = 0; i < hexStr.length; i++) {
      if (!(i % 4)) {
        test2.push(hexStr.substr(i, 4))
      }
    }
    console.log(test2); // ['0001', '0001', '0064', '0019', '017C', '000A', '0000', '0000']

    const test3 = test2.map(hex => {
      return hex2dex(hex);
    });
    console.log(test3) // ['1', '1', '100', '25', '380', '10', '0', '0']

    /* 示例四 */
    // 10进制转16进制  16进制转10进制
    console.log(dex2hex('1', 2), hex2dex('01')) // 01  1
    console.log(dex2hex('1', 4), hex2dex('0001')) // 0001  1
    console.log(dex2hex(1, 4), hex2dex('0001')) // 0001  1
    console.log(dex2hex(160, 4), hex2dex('00A0')) // 00A0  160
    console.log(dex2hex(1600, 4), hex2dex('0640')) // 0640  1600

  </script>
</body>
</html>

 

posted @ 2022-10-26 15:24  YINGYAN  阅读(55)  评论(0编辑  收藏  举报