# Converting Integer to Character Arduino

176,977

20

14

## Introduction: Converting Integer to Character Arduino

Converting an integer to character is an easy process. It involves first changing the integer into a string and then converting the string into a character array. The reason i am posting this short post is because just recently i realized that many people do not know how to convert an integer to a character, me included (well, but now i know).

I going to start with the easier option - Converting a character to integer, then move on to our point of interest.

## Step 1: Character to Integer

To convert a character to an integer you use this short statement:

int a;

char b;

a=b-'0';

That's it!

## Step 2: Integer to Character

This is more intricate than the last one. However, it is not as difficult as some (including me before I learnt how to do it) might think.

Here is the code:

int a=1;

char b;

String str;

str=String(a);

str.toCharArray(b,2);

By running this code, you will be able to convert an integer, into a character. However, as you will notice, the code above can only do conversions of numbers between -9 to 99 (thanks to a buddy who noted that on the comments). To be able to convert larger integers, change the array size of the character. Therefore, instead of:

char b;

you can use

char b;

to accomodate a n integer that has 5 digits. You can use any other array size depending on the size ofthe integer you want to convert.

http://www.arduino-hacks.com/converting-integer-to-character-vice-versa/

## 14 Discussions

The title is wrong. It should be: Converting an integer into a character array containing the decimal representation of the integer. (Your version is only working for integers [-9..99] by the way)

To convert an integer to a character you'd do:

int i=5;
char c = (char) i;

I agree that the char casting method you have described
above. However, the problem with it is that it does not transfer the real
value of the integer, which is 5, to the character. Instead, what it does is
transfer the ascii character equivalent of the value of the integer, to the
character variable.

For instance, I want you to upload this code onto your
arduino board and open the serial monitor and see the value that has been
passed to character c after the int to char conversion.

void setup()

{

Serial.begin(9600);//setting communication baud rate

int i=10; //initializing variables

char c=char(i);

Serial.print("Integer i = \t");

Serial.println(i);//display value of integer i

Serial.print("Character c = \t");

Serial.println(c);//display value of character c

}

void loop()

{

}

You will notice that there is no value of c displayed. This is because the
ascii character equivalent of 10 is a control action (backspace). You will get
the same results if you try to convert integer values ranging from 0 to 31 since they
are control actions and also value 32(space) and value 127(DEL). You can
confirm from the Ascii table below:

http://www.robelle.com/smugbook/ascii.html">http://www.robelle.com/smugbook/ascii.html

Now, change the values of int i to any value that is between 33 and 126
and when you run the program, the value of the Ascii character equivalent to the value of int i will
be displayed in the serial.

For instance if your use int i=123;
the character equivalent will be x.

The character might work as a normal integer during manipulations since
it will be using the hex equivalent value of x which is 123; and not the
character ‘x’. Therefore, you will be able to do something like i+c and still
get the correct integer answer 126.

However, this type of casting i.e. char
c=(char)i;
is only limited integers
that are between and inclusive of 0 and 127. Conversion of any integer above
the value of 127 using this method yields errors in manipulation and
comparison. It is due to this reason that it is advisable to use the method I described
above, since it can handle any size of integer and it transfers the true value
of the integer to the character and not the Ascii equivalent.

I will update the entire tutorial on http://www.arduino-hacks.com/converting-integer-to-character-vice-versa/
after an hour or so, to show you what I mean.