javascript - json 十六进制被转换为字符串
迷茫
迷茫 2017-04-11 13:26:27
0
4
465

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
];

代码如上

cmd 是通过PHP 转换得到的16进制编码但是JS 取到的时候已经变成了字符串
想实现将cmd 变成跟cmd1 一样的十六进制 截图蓝色部分为想要的效果 红色是数据内容

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(4)
巴扎黑
cmd.join().split(',').map(function(v) {return parseInt(v,16);});
PHPzhong

JSON.stringify()
括号里写变量

左手右手慢动作
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);
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!