{"id":323,"date":"2019-08-26T11:09:04","date_gmt":"2019-08-26T03:09:04","guid":{"rendered":"https:\/\/cloudbool.com\/archive\/?p=323"},"modified":"2019-08-26T11:09:04","modified_gmt":"2019-08-26T03:09:04","slug":"php-iot-hex-dec","status":"publish","type":"post","link":"https:\/\/cloudbool.com\/archive\/php-iot-hex-dec.html","title":{"rendered":"PHP\u7269\u8054\u7f51\u5f00\u53d1\u4e2d16\u8fdb\u5236\u8f6c10\u8fdb\u5236\u51e0\u4e2a\u76f8\u5173\u51fd\u6570"},"content":{"rendered":"<p>\u4e4b\u524d\u4e00\u76f4\u662f\u5728\u505a\u7535\u5546\u90a3\u4e00\u5757\uff0c\u57fa\u672c\u4e0a\u4e0d\u6d89\u53ca\u523016\u8fdb\u5236\uff0c\u4e0eAPP\u7aef\u6253\u4ea4\u9053\u90fd\u662f\u7528json\u683c\u5f0f\uff0c\u4f46\u662f\u81ea\u4ece\u6d89\u53ca\u5230\u7269\u8054\u7f51\uff0c\u9700\u8981\u4e0e\u786c\u4ef6\u7535\u8def\u677f\u6253\u4ea4\u9053\uff0c\u4e00\u4e9b\u60c5\u666f\u9700\u8981\u7528\u523016\u8fdb\u5236\uff0c\u5c24\u5176\u662f\u4e0e\u786c\u4ef6\u7aef\u901a\u4fe1\u7684\u60c5\u51b5\uff0c\u5bf9\u4e8e\u5355\u7247\u673a\u6765\u8bf4\uff0c\u76f8\u6bd4JSON\uff0c16\u8fdb\u5236\u6570\u7ec4\u53ef\u80fd\u662f\u6bd4\u8f83\u597d\u7684\u901a\u4fe1\u534f\u8bae\uff0c\u7f51\u4e0a\u53d1\u73b0PHP\u5f00\u53d1\u7269\u8054\u7f51\u76f8\u5173\u9879\u76ee\u4e2d16\u8fdb\u5236\u8d44\u6599\u76f8\u5bf9\u8f83\u5c11\uff0c\u8fd9\u91cc\u5206\u4eab\u4e0b\u6211\u8fd9\u8fb9\u4e0e\u786c\u4ef6\u901a\u4fe1\u76f8\u5173\u7684\u51e0\u4e2a\u91cd\u70b9\u51fd\u6570\u3002<br \/>\n<!--more--><\/p>\n<h2>\u901a\u4fe1\u524d\u63d0<\/h2>\n<p>\u9996\u5148\uff0c\u6211\u4eec\u670d\u52a1\u7aef\u4e0e\u786c\u4ef6\u7aef\u662f\u901a\u8fc7MQTT\u534f\u8bae\u8fdb\u884c\u901a\u4fe1\uff0c\u4e24\u7aef\u5404\u76d1\u542c\u4e00\u4e2atopic\uff0c\u4e00\u4e2a\u6536\u4e00\u4e2a\u53d1\uff0c\u53cc\u65b9\u4f7f\u752816\u8fdb\u5236\u6570\u7ec4\u8fdb\u884c\u901a\u4fe1\uff0c\u5982\u679c\u6211\u8fd9\u53e5\u8bdd\u6ca1\u6709\u8868\u8fbe\u6e05\u695a\uff0c\u90a3\u4e0b\u9762\u8fd9\u5f20\u56fe\u5e94\u8be5\u80fd\u66f4\u660e\u4e86\uff1a<br \/>\n<img decoding=\"async\" src=\"https:\/\/x.cloudbool.com\/img\/2019\/08\/26\/mqtt-hex-dec.png\" alt=\"mqtt-hex-dec.png\" \/><br \/>\n\u5de6\u4fa7\u662f\u7528\u6765\u6a21\u62df\u786c\u4ef6\u7aef\u901a\u8fc7MQTT Broker\u7ed9\u670d\u52a1\u7aef\u53d1\u9001\u6570\u636e\uff0c\u4e2d\u95f4\u90a3\u4e00\u5217\u662f\u670d\u52a1\u7aef\u76d1\u542c\u7279\u5b9a\u7684topic\u7528\u6765\u63a5\u6536\u6240\u6709\u786c\u4ef6\u53d1\u9001\u8fc7\u6765\u7684\u6d88\u606f\uff0c\u6700\u53f3\u4fa7\u90a3\u4e00\u5217\u662f\u7528\u6765\u67e5\u770b\u6307\u5b9a\u786c\u4ef6\u53d1\u9001\u7684\u6d88\u606f\u3002<br \/>\n\u5176\u4e2d\uff0c\u786c\u4ef6\u53d1\u9001\u7684\u6d88\u606f\u683c\u5f0f\u5c31\u5982\u4e0a\u56fe\u5de6\u4fa7\u7ea2\u8272\u6846\u6846\u5708\u8d77\u7684\u90a3\u6837\uff0cPayload Type\u662fHex Array\uff0c\u670d\u52a1\u7aef\u63a5\u6536\u7684\u4e5f\u5c31\u662f16\u8fdb\u5236\u6570\u7ec4\u5408\u5e76\u6210\u7684\u5b57\u7b26\u4e32\u3002<br \/>\n\u672c\u6587\u6240\u8bf4\u7684\u51e0\u4e2a\u76f8\u5173\u51fd\u6570\u4e5f\u662f\u57fa\u4e8e\u8fd9\u79cd\u901a\u4fe1\u534f\u8bae\u3002<\/p>\n<h2>PHP\u76f8\u5173\u51fd\u6570<\/h2>\n<p>\u6d89\u53ca\u5230\u7684\u51fd\u6570\u5171\u6709\u5982\u4e0b\u51e0\u4e2a\uff0c\u76f8\u5173\u51fd\u6570\u7684\u4f5c\u7528\u8be6\u60c5\u8bf7\u67e5\u9605PHP\u7684\u6587\u6863\uff0c\u8bf4\u7684\u80af\u5b9a\u6bd4\u6211\u6e05\u695a\uff1a<\/p>\n<ul>\n<li>hexdec<\/li>\n<li>sprintf<\/li>\n<li>chr<\/li>\n<li>base_convert<\/li>\n<\/ul>\n<h2>\u9ad8\u4f4e\u5b57\u8282\u8f6c\u6362<\/h2>\n<p>\u8fd9\u91cc\u8bf4\u7684\u9ad8\u4f4e\u5b57\u8282\uff0c\u662f\u6307\u7528\u4e24\u4e2a16\u8fdb\u5236\u6570\u5b57\u7ec4\u5408\u6210\u4e00\u4e2a10\u8fdb\u5236\u6570\u5b57\uff0c\u6bd4\u59820x01,0x01\u6309\u7167\u8fd9\u79cd\u8f6c\u6362\u65b9\u5f0f\u8f6c\u6362\u621010\u8fdb\u5236\u5c31\u662f257\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code>    \/**\n     * \u9ad8\u4f4e\u5b57\u8282\u7684\u4e24\u4e2a16\u8fdb\u5236\u6570\u8f6c\u6362\u621010\u8fdb\u5236\n     * @param $byte1\n     * @param $byte2\n     * @return float|int\n     *\/\n    static public function intFrom2ByteHex($byte1, $byte2)\n    {\n        return hexdec($byte1) * 256 + hexdec($byte2);\n    }\n<\/code><\/pre>\n<h2>16\u8fdb\u5236\u5b57\u7b26\u4e32\u8f6c\u6362\u621016\u8fdb\u5236\u6570\u7ec4<\/h2>\n<p>\u867d\u7136\u8bbe\u5907\u7aef\u53d1\u9001\u7684\u662f16\u8fdb\u5236\u6570\u7ec4\uff0c\u4f46\u662f\u670d\u52a1\u7aef\u8fd9\u8fb9\u63a5\u6536\u5230\u7684\u5374\u662f16\u8fdb\u5236\u5b57\u7b26\u4e32\uff0c\u5982\u679c\u4e0b\u4e00\u6b65\u9700\u8981\u6309\u5b57\u8282\u8fdb\u884c\u4e1a\u52a1\u5904\u7406\uff0c\u53ef\u901a\u8fc7\u4e0b\u9762\u4ee3\u7801\u8fdb\u884c\u5c0616\u8fdb\u5236\u5b57\u7b26\u4e32\u5207\u5272\u621016\u8fdb\u5236\u6570\u7ec4\uff1a<\/p>\n<pre><code>\/**\n     * MQTT\u6536\u5230\u768416\u5b57\u7b26\u4e32\u8f6c\u6362\u621016\u8fdb\u5236\u6570\u7ec4\n     * @param $data\n     * @return array\n     *\/\n    static public function dataToHexArray($data)\n    {\n        \/\/\u6309\u5b57\u7b26\u5206\u5272\u768416\u8fdb\u5236\u6570\u7ec4\n        $hexArrayOrig = str_split($data, 1);\n        $hexArray = [];\n        foreach ($hexArrayOrig as $item) {\n            $hexArray[] = sprintf(\"%02X\", ord($item));\n        }\n        return $hexArray;\n    }\n<\/code><\/pre>\n<h2>10\u8fdb\u5236\u6570\u7ec4\u8f6c\u6362\u621016\u8fdb\u5236\u5b57\u7b26\u4e32<\/h2>\n<p>\u5728\u670d\u52a1\u7aef\u5904\u7406\u5b8c\u4e1a\u52a1\u4e4b\u540e\uff0c\u6709\u70b9\u65f6\u5019\u9700\u8981\u7ed9\u8bbe\u5907\u7aef\u56de\u590d\u6d88\u606f\u6216\u8005\u4e0b\u53d1\u6307\u4ee4\uff0c\u5c31\u9700\u8981\u548c\u4e0a\u65b916\u8fdb\u5236\u8f6c10\u8fdb\u5236\u76f8\u53cd\u5904\u7406\uff0c\u5c0610\u8fdb\u5236\u6570\u7ec4\u8f6c\u6362\u621016\u8fdb\u5236\u5b57\u7b26\u4e32\u7136\u540e\u901a\u8fc7MQTT\u534f\u8bae\u53d1\u9001\u7ed9\u786c\u4ef6\u7aef\uff0c\u76f8\u5173\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code>    \/**\n     * 10\u8fdb\u5236\u6570\u7ec4\u8f6c16\u8fdb\u5236\u5b57\u7b26\u4e32\n     * @param $array\n     * @return string\n     *\/\n    static public function decArrayToHexStr($array)\n    {\n        $hexArray = [];\n        foreach ($array as $item) {\n            $hexArray[] = chr(intval($item, 16));\n        }\n        $hexStr = join('', $hexArray);\n        return $hexStr;\n    }\n<\/code><\/pre>\n<h2>10\u8fdb\u5236\u6570\u7ec4\u8f6c16\u8fdb\u5236\u6570\u7ec4<\/h2>\n<p>\u4e0a\u9762\u4e00\u4e2a\u51fd\u6570\u662f10\u8fdb\u5236\u6570\u7ec4\u8f6c16\u8fdb\u5236\u5b57\u7b26\u4e32\uff0c\u8fd9\u4e2a\u662f\u8f6c\u6570\u7ec4\uff0c\u76f8\u5bf9\u6765\u8bf4\u66f4\u7b80\u5355\uff1a<\/p>\n<pre><code>        static public function hexArray2DecArray($hexArray)\n    {\n        $decArray = [];\n        foreach ($hexArray as $item) {\n            $decArray[] = hexdec($item);\n        }\n        return $decArray;\n    }\n<\/code><\/pre>\n<h2>\u6700\u540e<\/h2>\n<p>\u4ee5\u4e0a\u51e0\u4e2a\u51fd\u6570\u90fd\u662f\u6211\u5728\u5de5\u4f5c\u4e2d\u4f7f\u7528\u603b\u7ed3\u51fa\u6765\u7684\uff0c\u4e0d\u4e00\u5b9a\u9002\u5408\u522b\u7684\u60c5\u51b5\uff0c\u4f7f\u7528\u4e4b\u524d\u5e94\u8003\u8651\u5177\u4f53\u60c5\u51b5\uff0c\u5982\u679c\u6709\u66f4\u597d\u7684\u5b9e\u73b0\u65b9\u5f0f\uff0c\u4e5f\u53ef\u4ee5\u8bc4\u8bba\u533a\u6216\u8005\u90ae\u4ef6\u4ea4\u6d41\u4e0b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e4b\u524d\u4e00\u76f4\u662f\u5728\u505a\u7535\u5546\u90a3\u4e00\u5757\uff0c\u57fa\u672c\u4e0a\u4e0d\u6d89\u53ca\u523016\u8fdb\u5236\uff0c\u4e0eAPP\u7aef\u6253\u4ea4\u9053\u90fd\u662f\u7528json\u683c\u5f0f\uff0c\u4f46\u662f\u81ea\u4ece\u6d89\u53ca\u5230\u7269\u8054\u7f51\uff0c\u9700 &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"\" href=\"https:\/\/cloudbool.com\/archive\/php-iot-hex-dec.html\"> <span class=\"screen-reader-text\">PHP\u7269\u8054\u7f51\u5f00\u53d1\u4e2d16\u8fdb\u5236\u8f6c10\u8fdb\u5236\u51e0\u4e2a\u76f8\u5173\u51fd\u6570<\/span> \u67e5\u770b\u5168\u6587 &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""}},"footnotes":""},"categories":[8],"tags":[74],"class_list":["post-323","post","type-post","status-publish","format-standard","hentry","category-tech","tag-mqtt"],"_links":{"self":[{"href":"https:\/\/cloudbool.com\/archive\/wp-json\/wp\/v2\/posts\/323","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cloudbool.com\/archive\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cloudbool.com\/archive\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cloudbool.com\/archive\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cloudbool.com\/archive\/wp-json\/wp\/v2\/comments?post=323"}],"version-history":[{"count":0,"href":"https:\/\/cloudbool.com\/archive\/wp-json\/wp\/v2\/posts\/323\/revisions"}],"wp:attachment":[{"href":"https:\/\/cloudbool.com\/archive\/wp-json\/wp\/v2\/media?parent=323"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cloudbool.com\/archive\/wp-json\/wp\/v2\/categories?post=323"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cloudbool.com\/archive\/wp-json\/wp\/v2\/tags?post=323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}