Converting Arduino char to int Guide for Beginners

Converting Arduino char to int Guide for Beginners

Converting int integer variables to char variables in the Arduino IDE is a fairly simple process but worthy of a separate reminder. The hardest thing to understand is why the Char type is needed at all. Let me remind you that it is a one-byte character type.

To convert Int into Char, you must first convert an integer into String and then convert String into an array of Char. It’s so twisted on purpose so that no one would guess.

Let’s start with the more straightforward option of converting a Char character to an Integer and then move on to the basic question.

Step 1: Char to Integer

Use the following short combination to convert char to integer:

int a;
char b;

And that’s it!

Step 2: Integer to Char

The next example will use the auxiliary variable String. And the hardest part here is the conversion to an array of characters using a special function.

The code looks like this:

int a=1;
char b[2];
String str;

Using this example, you will convert an integer to char. However, the resulting array will only hold numbers between -9 and 99. For more digits, you would need to increase the size of the array, and instead of writing

char b[2];

You will have to insert

char b[5];

That way, you can display an integer that takes up five characters. You can also use an array of char and other lengths.

Related Video: Simple number conversion with String

Thank you for your attention! See you soon!