|
楼主 |
发表于 2020-7-9 22:01:52
|
显示全部楼层
解决了,
function InsertString(t, c, n) {
var r = new Array([]);
for (var i = 0; i * 2 < t.length; i++) {
r.push(t.substr(i * 2, n));
}
return r.join(c);
}
function FillString(t, c, n, b) {
if ((t === "") || (c.length != 1) || (n <= t.length)) {
return t;
}
var l = t.length;
for (var i = 0; i < n - l; i++) {
if (b === true) {
t = c + t;
}
else {
t += c;
}
}
return t;
}
function SingleToHex(t) {
if (t === "") {
return "";
}
t = parseFloat(t);
if (isNaN(t) === true) {
return "Error";
}
if (t === 0) {
return "00000000";
}
var s,
e,
m;
if (t > 0) {
s = 0;
}
else {
s = 1;
t = 0 - t;
}
m = t.toString(2);
if (m >= 1) {
if (m.indexOf(".") == -1) {
m = m + ".0";
}
e = m.indexOf(".") - 1;
}
else {
e = 1 - m.indexOf("1");
}
if (e >= 0) {
m = m.replace(".", "");
}
else {
m = m.substring(m.indexOf("1"));
}
if (m.length > 24) {
m = m.substr(0, 24);
}
else {
m = FillString(m, "0", 24, false)
}
m = m.substring(1);
e = (e + 127).toString(2);
e = FillString(e, "0", 8, true);
var r = parseInt(s + e + m, 2).toString(16);
r = FillString(r, "0", 8, true);
return InsertString(r, " ", 2).toUpperCase();
}
let buf = msg.payload;
let result = SingleToHex(buf);
msg.payload = result;
return msg; |
|