I am working on an encoder for LoRaWAN with JavaScript. I receive this field of data:
{“header”: 6,“sunrise”: -30,“sunset”: 30,“lat”: 65.500226,“long”: 24.833547}
And I need to encode in an hex message, my code is:
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));
Functions byteToHex / swap16 defined as:
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);
}
Testing it with return message = lon produces B3 09 in hex. Testing it with message = lon | lat <<8 return 96 BB 09 but result I am looking for is 96 19 B3 09 (composing lon + lat ).
Any tips? What I am doing wrong?