How to send float value and convert it into bytes

Nice! But since you asked for reviewing, some minor remarks which you might already know about:

While correct, the logical & 0xFF is not needed. With uint8_t mydata[message_size] anything but the least significant byte is discarded anyway. (The other bytes are not copied into an adjacent memory location; if that were the case one would not need 3 lines of code to start with.) So, the following would suffice:

mydata[0] = LatitudeBinary >> 16;
mydata[1] = LatitudeBinary >> 8;
mydata[2] = LatitudeBinary;

Though the short version might surely confuse readers at first, they’ll also run into many examples where no & 0xFF is used, so better prepare them. :wink:

Also, I guess you know it’s possible to send negative values, when taking care of sign extension in the payload function when sending fewer than 4 bytes for a value.

As for the decoder function:

Again, correct. But as you’re doing bitwise operations (which will make JavaScript return 32 bits rather than some number), I’d use that all the way. Apart from theoretically being faster, also no parentheses are needed in the following, as (unlike addition) the bitwise OR operator has lower precedence than the bitwise left shift:

var _lat = (bytes[0] << 16 | bytes[1] << 8 | bytes[2]) / 16777215.0 * 180.0 - 90;

And finally:

If you don’t care about the slash between the 9th and 10th value, then to nicely get leading zeroes, you could use something like:

var _inputHEX = bytes.map(function(b) {
  return ('0' + b.toString(16)).substr(-2);
}).join(' ');
1 Like