32 Bit overflow in Payload Format decoder

If you still prefer to do it in the payload format decoder:

Unsigned numbers are quite doable if you stay away from bitwise operators. To convert 4 bytes to a 32 bits unsigned integer, see uint32 in Abeeway Microtracker Decoder. Or, for a random number of bytes, but still limited to at most 6:

/**
 * Convert the array of bytes to an unsigned integer. 
 *
 * BEWARE: This is only safe up to 0x1FFFFFFFFFFFFF, so: 6 bytes.
 */
function uint(bytes) {
  return bytes.reduce(function(acc, b) {
    // Unsigned, so don't use any bitwise operators, which would always
    // yield a signed 32 bits integer instead.
    return acc * 0x100 + b;
  }, 0);
}

var i = 0;
// Convert the next 4 bytes to an unsigned integer number
var serial = uint(bytes.slice(i, i+=4));

If you want a 10 character decimal string with leading zeroes, then I’d first convert it to an unsigned number, like above, and then use ('000000000' + serial).substr(-10).

Or if you want a hexadecimal string, see hex in the same Abeeway Microtracker Decoder.

(Aside: see also How do I format my forum post? [HowTo] Thanks!)

1 Like