• 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:

    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.

    The other option you have is DataView:

    var d = new DataView(new ArrayBuffer(2));
    return new Uint8Array(d.buffer);

Avatar for Gordon @Gordon started