我正在使用 JavaScript 为 LoRaWAN 开发编码器。我收到这个数据字段:
{“header”: 6,“sunrise”: -30,“sunset”: 30,“lat”: 65.500226,“long”: 24.833547}
我需要在十六进制消息中编码,我的代码是:
var header = byteToHex(object.header);
var sunrise =byteToHex(object.sunrise);
var sunset = byteToHex(object.sunset);
var la = parseInt(object.lat100,10);
var lat = swap16(la);
var lo = parseInt(object.long100,10);
var lon = swap16(lo);
var message={};
if (object.header === 6){
message = (lon)|(lat<<8);
bytes = message;
}
return hexToBytes(bytes.toString(16));
函数 byteToHex / swap16 定义为:
function byteToHex(byte) {
var unsignedByte = byte & 0xff;
if (unsignedByte < 16) {
return ‘0’ + unsignedByte.toString(16);
} else {
return unsignedByte.toString(16);
}
}
function swap16(val) {
return ((val & 0xFF) << 8) | ((val >> 8) & 0xFF);
}
使用 return message = lon 对其进行测试会产生十六进制的 B3 09。用 message = lon | 测试它 lat <<8 返回 96 BB 09 但我正在寻找的结果是 96 19 B3 09 (组成 lon + lat )。
有小费吗?我做错了什么?