I am currently developing a LoRaWAN encoder using JavaScript. The data field received looks like this:
{“header”: 6,“sunrise”: -30,“sunset”: 30,“lat”: 65.500226,“long”: 24.833547}
My task is to encode this data into a hex message. Below is the code I have so far:
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));
The functions byteToHex / swap16 are defined as follows:
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);
}
When testing with return message = lon, I get B3 09 in hex. However, when testing with message = lon | lat <<8, I get 96 BB 09 instead of the desired result 96 19 B3 09 (combining lon + lat).
Any advice on what may be going wrong?