# 32 Bit overflow in Payload Format decoder

**URL:** <https://www.thethingsnetwork.org/forum/t/32-bit-overflow-in-payload-format-decoder/32850>\
**Category:** Application Development\
**Created:** [December 21, 2019, 4:37pm UTC](https://www.thethingsnetwork.org/forum/t/32-bit-overflow-in-payload-format-decoder/32850 "2019-12-21T16:37:55Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![arjanvanb](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/arjanvanb/32/46626_2.png) [@arjanvanb](https://www.thethingsnetwork.org/forum/u/arjanvanb)\
**Post date:** [December 21, 2019, 6:01pm UTC](https://www.thethingsnetwork.org/forum/t/32-bit-overflow-in-payload-format-decoder/32850/4 "2019-12-21T18:01:53Z")

</div>

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

Unsigned numbers are quite doable [if you stay away from bitwise operators](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#Signed_32-bit_integers). To convert 4 bytes to a 32 bits unsigned integer, see `uint32` in [Abeeway Microtracker Decoder](https://www.thethingsnetwork.org/forum/t/abeeway-microtracker-decoder/22473/2). Or, for a random number of bytes, but still limited to at most 6:

```javascript
/**
 * 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](https://www.thethingsnetwork.org/forum/t/abeeway-microtracker-decoder/22473/2).

(Aside: see also [How do I format my forum post? [HowTo]](https://www.thethingsnetwork.org/forum/t/how-do-i-format-my-forum-post-howto/8735) Thanks!)

---

_[View the full topic](https://www.thethingsnetwork.org/forum/t/32-bit-overflow-in-payload-format-decoder/32850)._
