{"id":3736,"date":"2024-11-10T13:04:16","date_gmt":"2024-11-10T05:04:16","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3736\/"},"modified":"2024-11-10T13:04:16","modified_gmt":"2024-11-10T05:04:16","slug":"javascript-%e5%ad%97%e8%8a%82%e6%95%b0%e7%bb%84%e5%a6%82%e4%bd%95%e8%bd%ac%e6%8d%a2%e4%b8%ba%e5%ad%97%e7%ac%a6%e4%b8%b2%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3736\/","title":{"rendered":"JavaScript \u5b57\u8282\u6570\u7ec4\u5982\u4f55\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173112879548685.jpg\" class=\"aligncenter\" title=\"JavaScript \u5b57\u8282\u6570\u7ec4\u5982\u4f55\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff1f\u63d2\u56fe\" alt=\"JavaScript \u5b57\u8282\u6570\u7ec4\u5982\u4f55\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>javascript \u5b57\u8282\u6570\u7ec4\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u7684\u5b9e\u73b0\u8fc7\u7a0b\u63a2\u7a76<\/strong><\/p>\n<p>\u5728 javascript \u4e2d\uff0c\u6709\u4e00\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\u53ef\u4ee5\u5c06\u5b57\u8282\u6570\u7ec4\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>function byteToString(arr) {\n  if(typeof arr === 'string') {\n    return arr;\n  }\n\n  var str = '',\n    _arr = arr;\n\n  for(var i = 0; i &lt; _arr.length; i++) {\n    var one = _arr[i].toString(2),\n      v = one.match(\/^1+?(?=0)\/);\n\n    if(v &amp;&amp; one.length == 8) {\n      var bytesLength = v[0].length;\n      var store = _arr[i].toString(2).slice(7 - bytesLength);\n\n      for(var st = 1; st &lt; bytesLength; st++) {\n        store += _arr[st + i].toString(2).slice(2);\n      }\n\n      str += String.fromCharCode(parseInt(store, 2));\n      i += bytesLength - 1;\n    } else {\n      str += String.fromCharCode(_arr[i]);\n    }\n  }\n\n  return str;\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5bf9\u4e8e v = one.match(\/^1+?(?=0)\/); \u8fd9\u884c\u4ee3\u7801\uff0c^1+?(?=0) \u8868\u8fbe\u5f0f\u4ece\u7b2c\u4e00\u4e2a 1 \u5230\u7b2c\u4e00\u4e2a 0 \u4e4b\u95f4\u5339\u914d\u8fde\u7eed 1 \u7684\u6570\u91cf\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u5bf9\u4e8e\u5b57\u8282\u503c 1001 0100\uff1a<\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<ul>\n<li>\u8f6c\u6362\u540e\u7684\u4e8c\u8fdb\u5236\u5b57\u7b26\u4e32 one \u4e3a 1111101000000000\u3002<\/li>\n<li>one.match(\/^1+?(?=0)\/) \u8fd4\u56de 11111\uff0c\u8868\u793a\u4ece\u7b2c\u4e00\u4e2a 1 \u5230\u7b2c\u4e00\u4e2a 0 \u6709 5 \u4e2a\u8fde\u7eed 1\u3002<\/li>\n<li>\u56e0\u6b64\uff0cv \u7b49\u4e8e 5\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fJavaScript \u5b57\u8282\u6570\u7ec4\u5982\u4f55\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>javascript \u5b57\u8282\u6570\u7ec4\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u7684\u5b9e\u73b0\u8fc7\u7a0b\u63a2\u7a76 \u5728 javascript \u4e2d\uff0c\u6709\u4e00\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\u53ef\u4ee5\u5c06\u5b57\u8282\u6570\u7ec4\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002\u5982\u4e0b\u6240\u793a\uff1a function byteToString(arr) { if(typeof arr === &#8216;string&#8217;) { return arr; } var str = &#8221;, _arr = arr; for(var i = 0; i &lt; _arr.length; i++) { var one = _arr[i].toString(2), v = one.match(\/^1+?(?=0)\/); if(v &amp;&amp; one.length == 8) { var bytesLength = v[0].length; var store = _arr[i].toString(2).slice(7 &#8211; bytesLength); [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-3736","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3736","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=3736"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3736\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3736"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3736"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3736"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}