I use Qt serial to send a command through a serial port. The command contains a a signed 16 bit integer (xx) that can range from -4096 to 4096 with the high byte send first. The command is created as follows:
int16_t xi = 0;
for (int i=0; i<280; i++){
xi = i;
// index = 0 1 2 3 4 5
// command = {'A','w','x','x','L','H'}
unsigned char command[6] = {'A','w','x','x','L','H'};
command[2] = xi >> 8;
command[3] = xi & 0xFF;
int len = strlen((char*)command);
qDebug()<<"strlen: "<<len;
}
The command works fine for all values except between 0 and 256. I don't understand why I get a string with a length of 2 for values between 0 and 256? What I'm doing wrong? Any help is appreciated.
Edit: How could I change command[2] = xi >> 8;
(or some other part) to avoid truncation and to get the full command for all integers between -4096 and 4096?