The javascript-json hexadecimal format is converted to a string.

Source: Internet
Author: User
{Code ...} {code ...} {code ...} {code ...} the code above refers to the hexadecimal code obtained through PHP conversion. However, when JavaScript gets the code, it has become a string and you want to convert cmd to the hexadecimal Blue part like cmd1 for the desired effect. red indicates the data content.

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=[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];
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"];
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 above

Cmd is a hexadecimal code obtained through PHP Conversion. However, when JavaScript is obtained, it has become a string.
To convert cmd to the hexadecimal blue part of cmd1, the desired result is red, which is the data content.

Reply content:

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=[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];
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"];
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 above

Cmd is a hexadecimal code obtained through PHP Conversion. However, when JavaScript is obtained, it has become a string.
To convert cmd to the hexadecimal blue part of cmd1, the desired result is red, which is the data content.

cmd.join().split(',').map(function(v) {return parseInt(v,16);});

JSON. stringify ()
Variable in brackets

cmd.join().split(',').map(function(v) {return parseInt(v);});

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 = []; // convert to an integer array cmd. forEach (function (d) {d. split (","). forEach (function (d) {partition 1.push (parseInt (d) ;}); // Print the output string in hexadecimal format var output = "var cmd1 = ["; statement 1.foreach (function (d, I) {var _ u = d. toString (16 ). toUpperCase (); output + = "0x" + (_ u. length <2? "0" + _ u: _ u); if (I! = Bytes 1.length-1) output + = "," ;}); output + = "]"; // output string: var cmd1 = [0x02,0x03, 0xB4, 0xB4, 0 x, 0 x, 0 x, 0x02, 0x6E, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0 x, 0x05] console. log (output );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.