# Trouble with decoding uint64

**URL:** <https://www.thethingsnetwork.org/forum/t/trouble-with-decoding-uint64/21768>\
**Category:** Application Development\
**Created:** [January 23, 2019, 9:58pm UTC](https://www.thethingsnetwork.org/forum/t/trouble-with-decoding-uint64/21768 "2019-01-23T21:58:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![diygoodies](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/diygoodies/32/41692_2.png) [@diygoodies](https://www.thethingsnetwork.org/forum/u/diygoodies)\
**Post date:** [January 23, 2019, 9:58pm UTC](https://www.thethingsnetwork.org/forum/t/trouble-with-decoding-uint64/21768/1 "2019-01-23T21:58:32Z")

</div>

Hi everyone. I have trouble with decoding uint64.  
For example I need to decode next sequence of bytes.  
00 00 00 2E FC 83 ED 7D 00 00 00 BE  
where 00 00 00 2E FC 83 ED 7D is the value in unsigned int 64 format  
I am trying to decode it with this code

```
function Decoder(bytes) {

  var s=bytes[0] << 56 |bytes[1] << 48 |bytes[2] << 40|bytes[3] << 32|bytes[4] << 24|bytes[5] << 16|bytes[6] << 8|bytes[7];
  var t = bytes[8] << 24 |bytes[9] << 16 |bytes[10] << 8 |bytes[11];

  return {
    serial: s,
    temperature: t 
  }
}

```

But I get wrong decoded values  
{  
“serial”: -58462849,  
“temperature”: 190  
}  
serial must be 201805000061.  
I just cant understand what is wrong here.

Playing some more time I discovered that next code decode sequence in wright way  
function Decoder(bytes) {

```
  var sh = bytes[0] << 24 |bytes[1] << 16 |bytes[2] << 8 |bytes[3];
  var sl = bytes[4] << 24 |bytes[5] << 16 |bytes[6] << 8 |bytes[7];
  sh=(sh*0x100000000);
  var s=sh + sl+0x100000000;
  var t = bytes[8] << 24 |bytes[9] << 16 |bytes[10] << 8 |bytes[11];

  return {
    serial: s,
    temperature: t 
  }
}

```

The result is  
{  
“serial”: 201805000061,  
“temperature”: 190  
}  
But this code is too bulky and ugly. Help me please how to decode uint64 in correct way

---

<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:** [January 23, 2019, 11:12pm UTC](https://www.thethingsnetwork.org/forum/t/trouble-with-decoding-uint64/21768/3 "2019-01-23T23:12:54Z")

</div>

A payload format Decoder in TTN Console is a JavaScript function, and JavaScript has no notion of 64 bits integers. Instead, [all numbers are floating point](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type), and have [a maximum integer precision of 53 bits](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER). (So, your “bulky” version will fail when not starting with 0x0000 like in your example.) On top of that, JavaScript’s bitwise operators always work on 32 bits numbers.

You’ll have to convert the bytes into a string value, probably easiest done in a hexadecimal represenation:

```javascript
var s = bytes.slice(0, 8).map(function(b) {
  // Return the byte in a hexadecimal representation with a leading zero
  return ("0" + b.toString(16)).substr(-2);
}).join("").toUpperCase();

```

As an aside: the LoRaWAN DevEUI can probably take care of supplying (or mapping to) the serial you’re after? Saves you 8 bytes in the application’s payload.

---

<div class="post-metadata">

**Author:** ![system](https://www.thethingsnetwork.org/forum/uploads/default/original/3X/6/2/6250ea44fd2b0d6259489e1a92748df681e1f970.png) [@system](https://www.thethingsnetwork.org/forum/u/system)\
**Post date:** [March 24, 2019, 11:12pm UTC](https://www.thethingsnetwork.org/forum/t/trouble-with-decoding-uint64/21768/4 "2019-03-24T23:12:56Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
