loading

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/

<p>thank you very much it helped me a lot.</p>
<p>Hello, i have a problem with numbers over 9999 it just returns random numbers. </p><p>This is what i'm doing:</p><p>int a=9999;<br> char b[8];<br> sprintf(b,&quot;%d&quot;,a);</p><p>Thanks!</p>
<p>The title is wrong. It should be:<em> Converting an integer into a character array containing the decimal representation of the integer.</em> (Your version is only working for integers [-9..99] by the way)</p><p>To convert an integer to a character you'd do:</p><blockquote>int i=5;<br>char c = (char) i;<br></blockquote>
<p>I agree that the char casting method you have described <br>above. However, the problem with it is that it does not transfer the real <br>value of the integer, which is 5, to the character. Instead, what it does is <br>transfer the ascii character equivalent of the value of the integer, to the <br>character variable.</p><p>For instance, I want you to upload this code onto your <br>arduino board and open the serial monitor and see the value that has been <br>passed to character c after the int to char conversion. </p><p><em>void setup()</em></p><p><em>{</em></p><p><em> <br>Serial.begin(9600);//setting communication baud rate</em></p><p><em> <br>int i=10; //initializing variables</em></p><p><em> <br>char c=char(i); </em></p><p><em> <br>Serial.print(&quot;Integer i = \t&quot;);</em></p><p><em> <br>Serial.println(i);//display value of integer i</em></p><p><em> <br>Serial.print(&quot;Character c = \t&quot;);</em></p><p><em> <br>Serial.println(c);//display value of character c</em></p><p><em>}</em></p><p><em>void loop()</em></p><p><em>{</em></p><p><em>} </em></p><p>You will notice that there is no value of c displayed. This is because the <br>ascii character equivalent of 10 is a control action (backspace). You will get <br>the same results if you try to convert integer values ranging from 0 to 31 since they <br>are control actions and also value 32(space) and value 127(DEL). You can <br>confirm from the Ascii table below:</p><p>http://www.robelle.com/smugbook/ascii.html&quot;&gt;http://www.robelle.com/smugbook/ascii.html</p><p>Now, change the values of int i to any value that is between 33 and 126 <br>and when you run the program, the value of the Ascii character equivalent to the value of int i will <br>be displayed in the serial.</p><p>For instance if your use <strong>int i=123;</strong> <br>the character equivalent will be <strong>x</strong>.</p><p>The character might work as a normal integer during manipulations since <br>it will be using the hex equivalent value of x which is 123; and not the <br>character &lsquo;x&rsquo;. Therefore, you will be able to do something like <strong>i+c</strong> and still <br>get the correct integer answer <strong>126</strong>.</p><p>However, this type of casting i.e. <strong>char <br>c=(char)i; </strong> is only limited integers <br>that are between and inclusive of 0 and 127. Conversion of any integer above <br>the value of 127 using this method yields errors in manipulation and <br>comparison. It is due to this reason that it is advisable to use the method I described <br>above, since it can handle any size of integer and it transfers the true value <br>of the integer to the character and not the Ascii equivalent.</p><p>I will update the entire tutorial on <a href="http://www.arduino-hacks.com/converting-integer-to-character-vice-versa/" rel="nofollow">http://www.arduino-hacks.com/converting-integer-to-character-vice-versa/</a> <br>after an hour or so, to show you what I mean.</p>
<p>The link <a href="http://www.arduino-hacks.com/converting-integer-to-character-vice-versa/" rel="nofollow">http://www.arduino-hacks.com/converting-integer-to-character-vice-versa/</a> does not work any more.</p>
<p>VERY GOOD. thanks. </p>
<p>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?)</p><p>int a=100;</p><p>char b[10];</p><p>sprinf(b,&quot;%d&quot;,a);</p>
<p>yeah sprintf works just fine</p>
<p>Another win for sprintf if you can use it is that the String object is rather large and String has been historically buggy. </p>
You don't mention that you are coding in arduino until the end. for all I know it could be matlab.
<p>point noted. I have changed the topic</p>

About This Instructable

100,766views

14favorites

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 server Configuring xBee in API mode 
Add instructable to: