{"id":991,"date":"2023-12-14T19:55:32","date_gmt":"2023-12-14T11:55:32","guid":{"rendered":"https:\/\/www.fanhangame.ltd\/?p=991"},"modified":"2024-04-04T22:16:42","modified_gmt":"2024-04-04T14:16:42","slug":"js-16%e8%bf%9b%e5%88%b6%e5%ad%97%e7%ac%a6%e4%b8%b2%e4%b8%8e%e5%ad%97%e8%8a%82%e6%95%b0%e7%bb%84%e7%9a%84%e4%ba%92%e8%bd%ac","status":"publish","type":"post","link":"https:\/\/www.fanhangame.ltd\/?p=991","title":{"rendered":"JS 16\u8fdb\u5236\u5b57\u7b26\u4e32\u4e0e\u5b57\u8282\u6570\u7ec4\u7684\u4e92\u8f6c"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>function hexStringToByteArray(hexString) {\r\n  if (hexString.length % 2 !== 0) {\r\n    hexString = '0' + hexString;\r\n  }\r\n  const byteArray = &#91;];\r\n  for (let i = 0; i &lt; hexString.length; i += 2) {\r\n    const byte = parseInt(hexString.substr(i, 2), 16);\r\n    byteArray.push(byte);\r\n  }\r\n  return byteArray;\r\n}\n\nfunction byteArrayToHexString(byteArray) {\r\n  let hexString = '';\r\n  for (let i = 0; i &lt; byteArray.length; i++) {\r\n    hexString += byteArray&#91;i].toString(16).padStart(2, '0');\r\n  }\r\n  return hexString;\r\n}\r\n\n\/\/\u5b57\u7b26\u4e32\u8f6c\u5b57\u8282\u5e8f\u5217\r\n function stringToByte(str) {\r\n    var bytes = new Array();\r\n    var len, c;\r\n    len = str.length;\r\n    for (var i = 0; i &lt; len; i++) {\r\n        c = str.charCodeAt(i);\r\n        if (c >= 0x010000 &amp;&amp; c &lt;= 0x10FFFF) {\r\n            bytes.push(((c >> 18) &amp; 0x07) | 0xF0);\r\n            bytes.push(((c >> 12) &amp; 0x3F) | 0x80);\r\n            bytes.push(((c >> 6) &amp; 0x3F) | 0x80);\r\n            bytes.push((c &amp; 0x3F) | 0x80);\r\n        } else if (c >= 0x000800 &amp;&amp; c &lt;= 0x00FFFF) {\r\n            bytes.push(((c >> 12) &amp; 0x0F) | 0xE0);\r\n            bytes.push(((c >> 6) &amp; 0x3F) | 0x80);\r\n            bytes.push((c &amp; 0x3F) | 0x80);\r\n        } else if (c >= 0x000080 &amp;&amp; c &lt;= 0x0007FF) {\r\n            bytes.push(((c >> 6) &amp; 0x1F) | 0xC0);\r\n            bytes.push((c &amp; 0x3F) | 0x80);\r\n        } else {\r\n            bytes.push(c &amp; 0xFF);\r\n        }\r\n    }\r\n    return bytes;\r\n    }\r\n    \/\/\u5b57\u8282\u5e8f\u5217\u8f6cASCII\u7801\r\n\/\/&#91;0x24, 0x26, 0x28, 0x2A] ==> \"$&amp;C*\"\r\n function byteToString(arr) {\r\n    if (typeof arr === 'string') {\r\n        return arr;\r\n    }\r\n    var str = '',\r\n        _arr = arr;\r\n    for (var i = 0; i &lt; _arr.length; i++) {\r\n        var one = _arr&#91;i].toString(2),\r\n            v = one.match(\/^1+?(?=0)\/);\r\n        if (v &amp;&amp; one.length == 8) {\r\n            var bytesLength = v&#91;0].length;\r\n            var store = _arr&#91;i].toString(2).slice(7 - bytesLength);\r\n            for (var st = 1; st &lt; bytesLength; st++) {\r\n                store += _arr&#91;st + i].toString(2).slice(2);\r\n            }\r\n            str += String.fromCharCode(parseInt(store, 2));\r\n            i += bytesLength - 1;\r\n        } else {\r\n            str += String.fromCharCode(_arr&#91;i]);\r\n        }\r\n    }\r\n    return str;\r\n    }<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":1067,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-991","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized","h-entry","h-as-article"],"_links":{"self":[{"href":"https:\/\/www.fanhangame.ltd\/index.php?rest_route=\/wp\/v2\/posts\/991","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.fanhangame.ltd\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fanhangame.ltd\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fanhangame.ltd\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fanhangame.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=991"}],"version-history":[{"count":1,"href":"https:\/\/www.fanhangame.ltd\/index.php?rest_route=\/wp\/v2\/posts\/991\/revisions"}],"predecessor-version":[{"id":992,"href":"https:\/\/www.fanhangame.ltd\/index.php?rest_route=\/wp\/v2\/posts\/991\/revisions\/992"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.fanhangame.ltd\/index.php?rest_route=\/wp\/v2\/media\/1067"}],"wp:attachment":[{"href":"https:\/\/www.fanhangame.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=991"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fanhangame.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=991"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fanhangame.ltd\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=991"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}