# Decodeng and converting payload

**URL:** <https://www.thethingsnetwork.org/forum/t/decodeng-and-converting-payload/20798>\
**Category:** Application Development\
**Created:** [December 18, 2018, 1:02am UTC](https://www.thethingsnetwork.org/forum/t/decodeng-and-converting-payload/20798 "2018-12-18T01:02:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![fmeylink](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/fmeylink/32/25844_2.png) [@fmeylink](https://www.thethingsnetwork.org/forum/u/fmeylink)\
**Post date:** [December 18, 2018, 1:02am UTC](https://www.thethingsnetwork.org/forum/t/decodeng-and-converting-payload/20798/1 "2018-12-18T01:02:34Z")

</div>

Hi,

My aplication send as payload temp and moisture as hex.

the actual payload now is: 16 1C  
16 is the temp 22 degrees 1C is the moisture 28%

How do i decode this 16 1C to te 22degrees and 28% ?  
I read the decoding payload for dummys topic, that did not help much ☹

---

<div class="post-metadata">

**Author:** ![dcmille](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/dcmille/32/25844_2.png) [@dcmille](https://www.thethingsnetwork.org/forum/u/dcmille)\
**Post date:** [December 18, 2018, 2:04am UTC](https://www.thethingsnetwork.org/forum/t/decodeng-and-converting-payload/20798/2 "2018-12-18T02:04:14Z")

</div>

Let javascript do the work for you:

```
decoded.Temperature = bytes[0];
decoded.Humidity = bytes[1];

```

Remember, a number is ultimately just a binary number stored inside the computer. It’s how that number is displayed that matters.  
Payload data is displayed in Hex bytes, but if you decode it using Payload Functions, it gets stored (and displayed) as a json object, so usually numbers will be printed as decimal.

N.B. Negative numbers tend to cause problems (they are stored as 2’s complement), so you might want to modify you code to add and substract 100 to the temperature.  
i.e. in the node: temp = temp + 100;  
And in the payload decoding: decoded.Temperature = bytes[0] - 100;  
That will give you a range from -100c to +155c, which is pretty safe for air temperature.

---

<div class="post-metadata">

**Author:** ![fmeylink](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/fmeylink/32/25844_2.png) [@fmeylink](https://www.thethingsnetwork.org/forum/u/fmeylink)\
**Post date:** [December 18, 2018, 8:52am UTC](https://www.thethingsnetwork.org/forum/t/decodeng-and-converting-payload/20798/3 "2018-12-18T08:52:16Z")

</div>

i made this:

```auto
decoded.Temperature = bytes[0];
decoded.Humidity = bytes[1];

```

got this answer:

Internal error: Decoder threw error: ReferenceError("‘decoded’ is not defined")

something went wrong …

---

<div class="post-metadata">

**Author:** ![UdLoRa](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/udlora/32/45330_2.png) [@UdLoRa](https://www.thethingsnetwork.org/forum/u/UdLoRa)\
**Post date:** [December 18, 2018, 9:27am UTC](https://www.thethingsnetwork.org/forum/t/decodeng-and-converting-payload/20798/4 "2018-12-18T09:27:42Z")

</div>

You must learn at least a minimum of programming and Javascript to develop an application… declare the variable first.

---

<div class="post-metadata">

**Author:** ![fmeylink](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/fmeylink/32/25844_2.png) [@fmeylink](https://www.thethingsnetwork.org/forum/u/fmeylink)\
**Post date:** [December 18, 2018, 2:54pm UTC](https://www.thethingsnetwork.org/forum/t/decodeng-and-converting-payload/20798/5 "2018-12-18T14:54:09Z")

</div>

ok ok  
I saw the youtube about this and now it works.  
Added another var that works but not al i like.

the value must be rounded to two decimals, how do i do that in java ?  
expamples failed…

i use: math.round (bat/1000)  
the result is a figure with no decimals.  
the result = 4 in stead of 4.145  
i want as a result 4.15

---

<div class="post-metadata">

**Author:** ![fmeylink](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/fmeylink/32/25844_2.png) [@fmeylink](https://www.thethingsnetwork.org/forum/u/fmeylink)\
**Post date:** [December 18, 2018, 3:30pm UTC](https://www.thethingsnetwork.org/forum/t/decodeng-and-converting-payload/20798/6 "2018-12-18T15:30:56Z")

</div>

got it 🙂

bat: Math.round ((bat/1000.0) \* 100.0) / 100.0 did the trick

---

<div class="post-metadata">

**Author:** ![kersing](https://www.thethingsnetwork.org/forum/user_avatar/www.thethingsnetwork.org/kersing/32/44777_2.png) [@kersing](https://www.thethingsnetwork.org/forum/u/kersing)\
**Post date:** [December 18, 2018, 5:22pm UTC](https://www.thethingsnetwork.org/forum/t/decodeng-and-converting-payload/20798/7 "2018-12-18T17:22:56Z")

</div>

How about:  
`Math.round(bat/10.0)/100.0`  
One less multiplication required.

BTW. Javascript is not Java. They share a couple of characters in the name, that is all they have in common.
