The bit shift is just a way to turn the two 8 bit values into one 8 bit one.
You're doing basically the right thing but n % 1 * 100 looks a bit weird to me... Something like this should work:
n % 1 * 100
function encodeFloat(num) { var d = Math.round(n*100); return [ d&255, d >> 8 ]; }
The &255 isn't required (it cuts of the top 8 bits) as we're writing an 8 bit array anyway, but it makes it easier to debug.
&255
The other option you have is DataView:
DataView
var d = new DataView(new ArrayBuffer(2)); d.setInt16(0,num*100,true) return new Uint8Array(d.buffer);
@Gordon started
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
The bit shift is just a way to turn the two 8 bit values into one 8 bit one.
You're doing basically the right thing but
n % 1 * 100
looks a bit weird to me... Something like this should work:The
&255
isn't required (it cuts of the top 8 bits) as we're writing an 8 bit array anyway, but it makes it easier to debug.The other option you have is
DataView
: