hello everyone, the serial number field of my electricity meter actually has 32 bits. But if I add bytes 0 to 3, I get an overflow from “8F FF FF FF”. At what point in my decoder do I have to declare the 32 bit number?
This value exceeds the range of the signed integers used by relevant javascript operations. It would fit int a 32-bit unsgined integer but that does not exist in all implementations.
It may be best to treat this as a string of hex digits, not a number. Given that it’s a serial number it’s not like you would be performing numeric operation on it, and you can construct it by string concatenation of either the octets or of two 16 bit values.
Sure, of course! Thank you for the solution. I read the 4 bytes individually and add them on my server via #!/bin/bash :-). In addition to the serial number, the display of the power consumption is 4 bytes long. Why it was made so big is questionable - but that’s how it is sometimes …
/**
* 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).