# Problem in sending negative values in the payload

**URL:** <https://www.thethingsnetwork.org/forum/t/problem-in-sending-negative-values-in-the-payload/35604>\
**Category:** LMIC\
**Created:** [March 24, 2020, 3:06am UTC](https://www.thethingsnetwork.org/forum/t/problem-in-sending-negative-values-in-the-payload/35604 "2020-03-24T03:06:47Z")\
**Posts on this page:** 1\
**Showing post:** 8

<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:** [March 24, 2020, 6:07pm UTC](https://www.thethingsnetwork.org/forum/t/problem-in-sending-negative-values-in-the-payload/35604/8 "2020-03-24T18:07:57Z")

</div>

> [@samdanisms](#):
>
> ```cpp
> LMIC_setTxData2(1, (uint8_t*)payload, sizeof(payload), 0);
> 
> ```
> 
> Again, could this be the issue?

Like @kersing already wrote: no. The above “cast” only tells the compiler that you know what you’re doing, and that whatever is in the variable `payload` can be handled as if it were of type `uint8_t`. That happens to be what this LMIC function expects, but your code has declared `payload` to be of a different type:

```cpp
byte payload[16];

```

Even though that is really the same (both `uint8_t` and `byte` define 8 bits unsigned integer values, from 0 thru 255), you need to tell the compiler that you know it’s okay. You could also use:

```cpp
uint8_t payload[16];
...
// No need to cast now:
LMIC_setTxData2(1, payload, sizeof(payload), 0);

```

So what’s your code doing? I’ve added some comments:

```cpp
// Each val holds the sum of 200 samples.
float val[6] = {0};
...
// Arduino int is 16 or 32 bits, depending on the board.
int value[8];
...
// Take the average of the 200 samples and multiply by 100, to
// send 12.35 as 1235. Add +1000 to always send positive values,
// hence assuming the average is larger than -1000. For a 16 bits
// unsigned integer, which supports 0 thru 65,535, this allows
// for actual averages between -10.00 and +645.35. To allow for,
// e.g., -300.00 thru +355.35, one would add 30,000 instead.
value[3]= (val[3] / 2.0) + 1000.0;
...
// Send MSB first
payload[6] = highByte(value[3]);
// ...and LSB next
payload[7] = lowByte(value[3]);

```

Like I linked to earlier, for the last two lines, [you could also use](https://www.thethingsnetwork.org/forum/t/decrypting-messages-for-dummies/4894/4):

```cpp
// MSB
payload[6] = value[3]>>8;
payload[7] = value[3];

```

All looks good so far. You’re using two bytes to send each value, [MSB first](https://en.wikipedia.org/wiki/Most_significant_byte). And you’re not sending negative values, so no magic needed in the decoder.

However, while decoding, you’re using:

```javascript
var LC = (bytes[6]<<15) | bytes[7];
...
return {
  ...
  LC: (LC-1000)/100,
  ...
}

```

That’s wrong: why shift the MSB 15 bits to the left when concatening the two bytes into a single value? I’d say this should read:

```javascript
var LC = bytes[6]<<8 | bytes[7];

```

(No parentheses needed either. It’s okay to add them, of course.)

If in the Arduino code you would _not_ add the 1,000, so if you would actually be sending _signed_ 16 bits values, then in a JavaScript decoder you would need:

```javascript
var LC = bytes[6]<<24>>16 | bytes[7];

```

See [Decrypting messages for dummies - #4 by arjanvanb](https://www.thethingsnetwork.org/forum/t/decrypting-messages-for-dummies/4894/4) that I linked to before.

---

_[View the full topic](https://www.thethingsnetwork.org/forum/t/problem-in-sending-negative-values-in-the-payload/35604)._
