json hex converted to string

WBOY
Release: 2016-10-22 00:14:09
Original
1187 people have browsed it

json hex converted to string

<code class="javascript">var cmd = ["0x02,0x03,0xB4,0xB4,0x00,0x06","0x00,0x02,0x6E,0x00,0x00,0x05","0x01,0x01,0x00,0x78,0x00,0x05","0x03,0x03,0x82,0x96,0x00,0x05"];</code>
Copy after login
Copy after login
<code class="javascript">var cmd1=[0x02, 0x03, 0xB4, 0xB4, 0xFF, 0xFF,0x02, 0x03, 0xB4, 0xB4, 0x00, 0x06,0x00, 0x02, 0x6E, 0x00, 0x00, 0x05,0x01, 0x01, 0x00, 0x78, 0x00, 0x05,0x03, 0x03, 0x82, 0x96, 0x00, 0x05];</code>
Copy after login
Copy after login
<code class="javascript">var test =["0x02,0x03,0xB4,0xB4,0x00,0x06","0x00,0x02,0x6E,0x00,0x00,0x05","0x01,0x01,0x00,0x78,0x00,0x05","0x03,0x03,0x82,0x96,0x00,0x05"];</code>
Copy after login
Copy after login
<code class="javascript">var cloud = [0x02, 0x03, 0xB4, 0xB4, 0xFF, 0xFF,
    0x02, 0x03, 0xB4, 0xB4, 0x00, 0x06,
    0x00, 0x02, 0x6E, 0x00, 0x00, 0x05,
    0x01, 0x01, 0x00, 0x78, 0x00, 0x05,
    0x03, 0x03, 0x82, 0x96, 0x00, 0x05
];</code>
Copy after login
Copy after login

The code is as above

cmd is a hexadecimal code converted through PHP, but it has been turned into a string when obtained by JS.
I want to turn cmd into the same hexadecimal screenshot as cmd1. The blue part is the desired effect. The red part is Data content

Reply content:

json hex converted to string

<code class="javascript">var cmd = ["0x02,0x03,0xB4,0xB4,0x00,0x06","0x00,0x02,0x6E,0x00,0x00,0x05","0x01,0x01,0x00,0x78,0x00,0x05","0x03,0x03,0x82,0x96,0x00,0x05"];</code>
Copy after login
Copy after login
<code class="javascript">var cmd1=[0x02, 0x03, 0xB4, 0xB4, 0xFF, 0xFF,0x02, 0x03, 0xB4, 0xB4, 0x00, 0x06,0x00, 0x02, 0x6E, 0x00, 0x00, 0x05,0x01, 0x01, 0x00, 0x78, 0x00, 0x05,0x03, 0x03, 0x82, 0x96, 0x00, 0x05];</code>
Copy after login
Copy after login
<code class="javascript">var test =["0x02,0x03,0xB4,0xB4,0x00,0x06","0x00,0x02,0x6E,0x00,0x00,0x05","0x01,0x01,0x00,0x78,0x00,0x05","0x03,0x03,0x82,0x96,0x00,0x05"];</code>
Copy after login
Copy after login
<code class="javascript">var cloud = [0x02, 0x03, 0xB4, 0xB4, 0xFF, 0xFF,
    0x02, 0x03, 0xB4, 0xB4, 0x00, 0x06,
    0x00, 0x02, 0x6E, 0x00, 0x00, 0x05,
    0x01, 0x01, 0x00, 0x78, 0x00, 0x05,
    0x03, 0x03, 0x82, 0x96, 0x00, 0x05
];</code>
Copy after login
Copy after login

The code is as above

cmd is a hexadecimal code converted through PHP, but it has been turned into a string when obtained by JS.
I want to turn cmd into the same hexadecimal screenshot as cmd1. The blue part is the desired effect. The red part is Data content

<code>cmd.join().split(',').map(function(v) {return parseInt(v,16);});
</code>
Copy after login

JSON.stringify()
Write variables in brackets

<code>cmd.join().split(',').map(function(v) {return parseInt(v);});</code>
Copy after login

<code>var cmd = ["0x02,0x03,0xB4,0xB4,0x00,0x06","0x00,0x02,0x6E,0x00,0x00,0x05","0x01,0x01,0x00,0x78,0x00,0x05","0x03,0x03,0x82,0x96,0x00,0x05"];
var cmd1 = [];
// 转换为整数数组
cmd.forEach(function(d){
    d.split(",").forEach(function(d){
        cmd1.push(parseInt(d));
    })
});

// 打印输出16进制格式字符串
var output = "var cmd1=[";
cmd1.forEach(function(d,i){
    var _u = d.toString(16).toUpperCase();
    output += "0x"+ (_u.length<2 ? "0"+_u : _u);
    if(i!=cmd1.length-1) output+=",";
});
output += "]";
// 输出字符串:var cmd1=[0x02,0x03,0xB4,0xB4,0x00,0x06,0x00,0x02,0x6E,0x00,0x00,0x05,0x01,0x01,0x00,0x78,0x00,0x05,0x03,0x03,0x82,0x96,0x00,0x05]
console.log(output);</code>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!