I am trying to convert an integer into two separate bytes and then recombine it (for efficient sending data via Lora). I can do it for positive long variables and integers, but not negative integers. I did read that the integer must be “signed” so I did it and still not luck. I am measuring temperature which could be positive or negative number.
Help please
signed int temperature = -157;
byte partTemp[2];
void setup()
{
Serial.begin(115200);
partTemp[0] = temperature >> 8;
partTemp[1] = temperature;
signed int pT1 = (signed int)partTemp[0]<<8;
signed int pT0 = (signed int)partTemp[1];
igned int combinedpT = pT1 | pT0;
Serial.println("Array method integer temperature -157");
Serial.println(combinedpT, HEX); //
Serial.println(combinedpT); //
}
void loop()
{
}
1 Answers
Hi Steven.
I’m not familiar with converting integers into bytes.
Can you read this article and see if it helps: https://www.thethingsnetwork.org/docs/devices/bytes.html#how-to-send-negative-numbers
I’m sorry that I can’t help much.
Regards,
Sara