Arduino

How to Convert Integer to String Arduino

Arduino is a microcontroller-based platform consisting of both software and hardware. Arduino programming is based upon the C/C++ language. Multiple functions used in Arduino are inherited from both these languages. This article will describe different ways of converting int into string using C functions in Arduino IDE.

Methods to Convert int to String in Arduino

Int to string conversion is the process of converting an integer data type to a string data type using the Arduino code. This is often necessary when working with programming languages, as different data types have different characteristics and methods of manipulation.

For example, an integer may be used to perform mathematical calculations, while a string may be used to hold text data. In order to use the data stored in an integer as a string, it must first be converted to the string data type.

Multiple C functions are available that can be used in Arduino programming for converting int to string. Following are the three ways that can convert any integer to a string:

1: Using String() Function

String function in Arduino programming is the simplest way of transforming int to string. String() function constructs an instance of string class. Using this function different data types can be converted to string including the int.

Syntax

String() function syntax:

String(val)

Parameters:

This function takes one parameter:

val: A variable to format into a string.

Returns:

It returns a string.

Example Code

Following is the example code where we initialize a variable “a” with int data type. After that a new string is initialized with the name myStr. Next using the String() function variable a is returned as an instance of string.

void setup() {

Serial.begin(9600)/*Baud rate for serial communication*/

int a = 1234;    /*Initialize a string with value 1234*/

String myStr;    /*New string is defined*/

myStr = String(a);   /*Convert Int to String*/

Serial.print("Integer Converted to String: ");

Serial.println(myStr);   /*Print string value on serial monitor*/

}

void loop() {

}

We can see the converter string in the IDE serial monitor.

2: Using sprintf() Function

The sprintf stands for String print. This function takes value from variables and stores it inside the buffer. To show the variable formatted as string inside that buffer we use the Serial.print() function.

Syntax

sprintf(buffer, "%d", myInt);

Parameters

This function takes two arguments.

  • First argument buffer will store the characters inside the array.
  • Second argument is the int variable or any other string which is to be stored.
  • %d is the format specifier for signed decimal integers. It tells sprintf() that what datatype of variable is to be stored inside the buffer.

Returns

It returns a buffer which stores a string.

For more detailed information on Arduino sprintf() function read the article How to Print Multiple Variables Using sprintf() function in Arduino Serial Monitor.

Example Code

In given code an int variable is initialized. After that using the sprintf() function this integer value is converted to string and stored inside the buffer.

void setup() {

Serial.begin(9600);       /*Baud rate for serial communication*/

int myInt = 123;            /*Int defined*/

char buffer[10];            /*buffer size defined*/

sprintf(buffer, "%d", myInt);        /*convert int to a string and store inside a buffer*/

String myString = String(buffer);

Serial.print("Integer Converted to String: ");

Serial.println(myString);            /*Print string value on serial monitor*/

}

void loop() {

}

Output represents the integer converted to string.

3: Using dtostrf() Function

Arduino dtostrf() function allows you to specify the minimum width and number of decimal places for the resulting string.

This function can pass a double value and convert it into a ASCII representation which is stored inside the string.

Syntax

dtostrf(floatValue, minStringWidth, numAfterDecimal, charBuf_to_store_string);

Parameters

This function takes 4 arguments:

  • Float value (Type double)
  • Min string width (Type char)
  • Number after decimal (Type char)
  • Char buffer (Type char)

Return

This function returns a new pointer towards the string converted from int.

For a more detailed guide on the dtostrf() function read the tutorial Arduino dtostrf() Function -Turn Your Floats into Strings.

Example Code

This will create a string with a minimum width of 6 characters and no decimal places, so the resulting string will be ”  123“.

void setup() {

Serial.begin(9600);       /*Baud rate for serial communication*/

int myInt = 123;            /*Int defined*/

char buffer[10];            /*buffer size defined*/

dtostrf(myInt, 6, 0, buffer); /*convert double and floating values to string*/

String myString = String(buffer);

Serial.print("Integer Converted to String: ");

Serial.println(myString);            /*Print string value on serial monitor*/

}

void loop() {

}

Output represents the string “   123”. Three left spaces are left as the width defined for the output string is 6.

Conclusion

Arduino programming takes most of its function from C/C++. To convert integer into string in Arduino programming three different functions are there that includes dtostrf(), sprintf(), and String(). Using these functions any of the integers can be converted into string and displayed on either serial monitor or some I2C LCD or OLED display.

About the author

Kashif

I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.