# how to convert a int to a char ?

Hello,

I am here with a problem. I can't convert a int to a char. Look at the sample below.

``````int kaboom = 2;
char boo = (char)kaboom;

void loop() {

if(boo == 50){
digitalWrite(5, HIGH);
}

}
``````

The if-statement is here always false when it should be normally true like in java.

you mean you want boo to be the ASCII representation of your int ?
why would that fit in only one char ? an int can be large, event if it's only one byte it can range from 0 to 255

if you know that your number kaboom is actually a single digit (0 to 9) then you can get the ASCII representation by adding the ASCII value of '0'.

``````int kaboom = 2;
char boo = '0' + kaboom:

if (boo == 50 ) {...} // same as if (boo == '2') {....}
``````

but I don't see the benefit in doing so, just compare kaboom with the number 2

J-M-L:
you mean you want boo to be the ASCII representation of your int ?
why would that fit in only one char ? an int can be large, event if it's only one byte it can range from 0 to 255

if you know that your number kaboom is actually a single digit (0 to 9) then you can get the ASCII representation by adding the ASCII value of '0'.

``````int kaboom = 2;
``````

char boo = '0' + kaboom:

if (boo == 50 ) {...} // same as if (boo == '2') {....}

``````

but I don't see the benefit in doing so, just compare kaboom with the number 2 :)
``````

J-M-L:
you mean you want boo to be the ASCII representation of your int ?
why would that fit in only one char ? an int can be large, event if it's only one byte it can range from 0 to 255

if you know that your number kaboom is actually a single digit (0 to 9) then you can get the ASCII representation by adding the ASCII value of '0'.

``````int kaboom = 2;
``````

char boo = '0' + kaboom:

if (boo == 50 ) {...} // same as if (boo == '2') {....}

``````

but I don't see the benefit in doing so, just compare kaboom with the number 2 :)
``````

Thank you for your answer, I begin to understand and for the anwer on your question: (what's the benefit?) well it's just for comparing the input of my bluetooth module since you can only get the input char by char. What's the best way to compare your input of the bluetooth module with an int according to you?

Does also someone know how I can get the numeric value of a char in an int ? Thus not the ASCII code but just the real number.

I would suggest to study Serial Input Basics to get started and understand how to deal with your Serial input.

if you only receive commands that fit on 1 byte, then it's easier and you can compare whatever you get (you need to know what comes in of course, ASCII or binary) and to test is just an

``````if (incomingByte == xxx) {...}
``````

iliwasel:
Does also someone know how I can get the numeric value of a char in an int ? Thus not the ASCII code but just the real number.

the opposite way.. if you have a char holding '3' (the ASCII code for the symbol 3) then just subtract '0' from it and you get the number...

an ASCII code is just another number to represent your digit

So the symbol '0' is coded 48, the symbol '1' is coded 49 etc...

when you do `char c = '1';` the compiler stores the ASCII code (because you rightly used single quotes around the symbol to denote a character) in c, so the value of c is 49.

to get the integer representation, you subtract 48 which is the ASCII code of '0' (as symbols for 0 to 9 are one after another in the table)

``````char c = '1'; // c holds the value 49
int v = c - '0'; // v holds de value 1
``````