byte to char

I’m trying to put the value of a byte in to a char array with the following code:

void setup() {
  Serial.begin(9600);
  readSD();
}

void readSD() {
  char responseSD[33];
  byte line = 0;
  byte charPos = 0;
  while (charPos < 10) {
    responseSD[charPos] = char(charPos);
    charPos++;
    responseSD[charPos] = '\0';
  }
  Serial.print("responseSD: ");
  Serial.println(responseSD);
}

void loop() {

}

but this doesn’t seem to work as I hoped.
This is the result:

responseSD:

And I hoped to get this as a result:

responseSD: 0123456789

Can someone explain to me what I am doing wrong?

This
char(charPos);
is a call to a function named char and passing charPos as a parameter.

Perhaps you meant
(char)charPos

That's only half your solution though. The cast to char from byte does not change the contents (value). If you expect the byte value of 1 to print as 1 you need to convert the byte value to ASCII, usually just by adding 48 or '0'.

Thanks. The +48 did the trick.

I tried char(charPos), but that didn't work.
char(charPos+48) does work.

DKWatson:
This
char(charPos);
is a call to a function named char and passing charPos as a parameter.

Perhaps you meant
(char)charPos

char(charPos) is a proper way to do a cast in C++. The old C syntax also works.

The 'old' C syntax is gcc8.1 C11 and the correct way to do a cast in C.