Converting Integer to Character Arduino

132,215

18

13

Published

Introduction: Converting Integer to Character Arduino

About: 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 and the world at large. Twitter handle: @arduinoha...

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/

Share

    Recommendations

    • Oil Contest

      Oil Contest
    • Water Contest

      Water Contest
    • Clocks Contest

      Clocks Contest

    13 Discussions

    Thank you very much !!! :D
    I was going nuts ! lol ;)

    It help me out !

    0
    user
    mosix

    6 months ago

    thank you very much, I was going crazy to solve this problem, thanks for the time you spent writing this post, and for have thought to write it! Thanks again and happy new year!

    thank you very much it helped me a lot.

    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!

    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;
    2 replies

    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.

    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);

    2 replies

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

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

    1 reply