<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>
<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>
<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>
<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>
코드는 위와 같습니다
cmd는 PHP를 통해 변환된 16진수 코드인데 JS에서 얻을 때 문자열로 변환되었습니다.
cmd를 cmd1과 동일한 16진수 코드로 변환하고 싶습니다. 빨간색 효과가 데이터 내용입니다
<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>
<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>
<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>
<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>
코드는 위와 같습니다
cmd는 PHP를 통해 변환된 16진수 코드인데 JS에서 얻을 때 문자열로 변환되었습니다.
cmd를 cmd1과 동일한 16진수 코드로 변환하고 싶습니다. 빨간색 효과가 데이터 내용입니다
<code>cmd.join().split(',').map(function(v) {return parseInt(v,16);}); </code>
JSON.stringify()
괄호 안에 변수 쓰기
<code>cmd.join().split(',').map(function(v) {return parseInt(v);});</code>
<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>