Convert char to uint16_t

In my Arduino project I get some char variables into an array from a serial connection (Xbee modul).
The recieving of the variables works but later I have to convert two variables of the array into one uint16_t variable because a PWM driver is connected and it has a transfer parameter what is composed of the two chars. This works with values from 0x0100 to 0x017F but if the highest Bit of the lower Byte is set, for example 0x0180 then I get 0xFF80. I do not understand why it does not work.

  1. initializing of the variables
char input[10] = {0}; //puffer für die Daten
char current; //puffer für aktuelles Zeichen
int incount = 0; //Index für Puffer
bool transfin = false; //Bool zum setzen ob Transfer erfolgreich war

2.recieve the data from XBee modul

if ((XBee.available() > 3))
    {
      while ((incount < 4) & (!transfin))
      {
        current = XBee.read(); //aktuelles Zeichen einlesen
        if (current != 88)
        {
          //schreiben der Daten in den Puffer
          input[incount] = current;
          incount++;
          //Serial.write("In Puffer schreiben");
        }
        else
        {
          // puffer mit NULL Zeichen abschließen, damit das Ende der Zeichenkette
          // durch string Operationen erkannt wird
          input[incount] = '\0';
          incount++;
          transfin = true;
          Serial.write("Ende Uebertragung\n");
        }
      }

3.send data to PWM driver

terg= input[2] | uint16_t(input[1]) << 8 ;
 for(int x=0;x<34;x+=2)
 {
     if(motor[x] == input[1])
     {
         pwm.setPWM(motor[x+1],0,terg);
     }
  }

With the array motor I choose the correct channel which shall be set.
Thanks for your help.

I do not understand why it does not work.

Google "sign extension"

Hello and welcome,

Can 0x80 (128) be stored successfully in a char ?

Your buffers are defined as signed by default, so the compiler is helping you when turning your negative char into a negative int.

If your buffers are unsinged, declare them as such and the compiler will properly extend the sign. This code demonstrates:

  char input[10] = {0}; //puffer für die Daten
  int motor[10] = {0}; //puffer für die Motor Daten
  unsigned char unsigned_input[10] = {0}; //Unsigned puffer für die Daten
  unsigned int unsigned_motor[10] = {0}; //Unsigned puffer für die Motor Daten

  input[0] = 0x80;
  unsigned_input[0] = 0x80;
  motor[0] = input[0];
  unsigned_motor[0] = unsigned_input[0];

  Serial.println(motor[0], HEX);
  Serial.println(unsigned_motor[0], HEX);

The output of this code is:
FFFFFF80
80

Thanks for the quick replies.

Using unsigned char was the resolution for the issue. I hadn't thought of that fact.

krystof:
Thanks for the quick replies.

Using unsigned char was the resolution for the issue. I hadn't thought of that fact.

No problem.

BTW - did I modify your comments correctly?

I don't speak German so I guessed.

guix:
Hello and welcome,

Can 0x80 (128) be stored successfully in a char ?

Yes, and (no). :slight_smile: