Introduction: Converting Integer to Character Arduino

Picture of 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[2];

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[2];

you can use

char b[5];

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.

To learn more about how to convert one data type into another and get the code snipets, please visit this page:

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

Comments

dmistry5 (author)2017-03-03

thank you very much it helped me a lot.

Funguypro (author)2016-09-11

Hello, i have a problem with numbers over 9999 it just returns random numbers.

This is what i'm doing:

int a=9999;
char b[8];
sprintf(b,"%d",a);

Thanks!

verence (author)2014-02-26

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;
yhtomitsy (author)verence2014-02-26

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.

OmarC6 (author)2015-09-23

VERY GOOD. thanks.

maewert (author)2014-02-27

Any reason to not use sprintf? (I know that the Arduino version of sprinf does not support floats, but we're sticking to INTs here so it is fine?)

int a=100;

char b[10];

sprinf(b,"%d",a);

yhtomitsy (author)maewert2014-02-27

yeah sprintf works just fine

WileyS1 (author)yhtomitsy2014-08-10

Another win for sprintf if you can use it is that the String object is rather large and String has been historically buggy.

kakashibatosi (author)2014-02-26

You don't mention that you are coding in arduino until the end. for all I know it could be matlab.

yhtomitsy (author)kakashibatosi2014-02-26

point noted. I have changed the topic

About This Instructable

113,116views

15favorites

License:

Bio: I am an electrical engineer and an Arduino and electronics enthusiasts. I believe working with electricity should be fun as well as beneficial to engineers ... More »
More by yhtomitsy:RFduino - Sending and Receiving Data Via BLE Arduino redback simple serverConfiguring xBee in API mode
Add instructable to: