膜拜大佬,参照大佬的帖子终于弄好了,代码贴出来
var a= msg.payload[4];
var b= msg.payload[5];
var aa= (a.toString(16));
var aaa = hexNum(aa)
var bb= (b.toString(16));
var bbb = hexNum(bb)
function hexNum(str){
var newstr=str+''
if(newstr.length>0 && newstr.length<2){
newstr='0'+str
}
return newstr
}
var i= aaa+bbb;
let n = parseInt(i, 16).toString(2);
let bitNum=i.length*4;
if (n.length < bitNum) {
while (n.length < bitNum) {
n = "0" + n;
}
}
if (n.substring(0, 1) == "0") {
n = parseInt(n, 2);
msg.payload=n/100
return msg;
} else {
let n_unsign = "";
n = parseInt(n, 2) - 1;
n = n.toString(2);
n_unsign = n.substring(1, bitNum);
n_unsign = n_unsign.replace(/0/g, "z");
n_unsign = n_unsign.replace(/1/g, "0");
n_unsign = n_unsign.replace(/z/g, "1");
n = parseInt(-n_unsign, 2);
msg.payload=n/100
return msg;
}
|