Convert two bytes to short problems

Made this function that will take an byte array and convert it into an short array, but I don't get the correct answer.

short* bytesToShort(char* byteArr, int len) {

  static short data[50];

  short sh = 0;
  int k = 0;

  lengthCounter = 0;

  memset(data, 0, sizeof data);

  for (int i = 0; i < (len / 2); i++) {

    sh = 0;

    sh = (byteArr[k] << 8) | byteArr[k + 1];

    Serial.println(sh);

    k = k + 2;

    data[i] = sh;
    lengthCounter++;
  }

  return data;
}

If there is {0, F8} in the byteArr, is will show -8 as the output instead of 248, so I guess it reads it as {FF, F8} and I don't know why.

If there is {1, 22} it gives 290 as it should, so it is only a problem when there is a zero in the first element of the array.

...but you sent it a char array instead.

So stupid... thank you

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.