Javascript-json hexadecimal is converted to a string

Source: Internet
Author: User

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 as above

CMD is the 16 binary encoded by PHP conversion, but when JS is taken, it becomes a string.
Want to implement the hexadecimal blue part that turns cmd into the same cmd1 as the desired effect red 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 as above

CMD is the 16 binary encoded by PHP conversion, but when JS is taken, it becomes a string.
Want to implement the hexadecimal blue part that turns cmd into the same cmd1 as the desired effect red is the data content

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

Json.stringify ()
Write variables in parentheses

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 = [];// 转换为整数数组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);
  • 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.