Convert int to char


I have a sensor and I receive values from it between 0 and 255. I need to convert the readings from the sensor to an array of char.

For example, if my sensor reads 97 I need this 97 reading converted to the "a" (which is the representation of 97 in the ASCII table).

I need something like:

char temp[] = convertToASCII(97);

Which would be the same as:

chart temp[] = "a";

I read a lot in Google but could not find a way to do that.

Thank you.

int x = 97; char y = x;

It's just numbers. There is no "conversion" needed.

Regards, Ray L.

If you just want it reinterpreted as ASCII then that's something you would do when you display it. There's no difference in how it is stored.

int anInt = 97;   // 97 == 'a'

char aChar = anInt;


will print the "a" you are looking for. When print sees char type variables, it always outputs as ASCII.

97 and 'a' are two ways of writing exactly the same thing. There is no conversion needed. 97 IS 'a'.

char myString[] = {72 , 101 , 108 , 108 , 111 , 0};

Will print “Hello”

char string_to_send[2];
int i = 97;

string_to_send[0] = i;
string_to_send[1] = '\0'; nul to terminate the string.

I think that, any time you are trying to shove 2 bytes of information (i.e., an int) into a 1 byte bucket (i.e., a char), you should explicitly cast the assignment. It helps document what you're doing:

void setup() {
  // put your setup code here, to run once:
  char myStr[20];
  myStr[0] = (char) 97;      // Use a cast

Your code didn't work because you didn't define the myStr[] array.