Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert int to string on Arduino?

Tags:

arduino

How do I convert an int, n, to a string so that when I send it over the serial, it is sent as a string?

This is what I have so far:

int ledPin=13; int testerPin=8; int n=1;  char buf[10];  void setup() {     pinMode(ledPin, OUTPUT);     pinMode(testerPin, OUTPUT);     Serial.begin(115200); }  void loop() {     digitalWrite(ledPin, HIGH);     sprintf(buf, "Hello!%d", n);     Serial.println(buf);     delay(500);     digitalWrite(ledPin, LOW);     delay(500);      n++; } 
like image 700
user947659 Avatar asked Oct 26 '11 23:10

user947659


People also ask

What is Atoi in Arduino?

Description. The atoi() function converts a character string to an integer value. The input string is a sequence of characters that can be interpreted as a numeric value of the specified return type. The function stops reading the input string at the first character that it cannot recognize as part of a number.

How do I convert int to char?

Example 1: Java Program to Convert int to char char a = (char)num1; Here, we are using typecasting to covert an int type variable into the char type variable. To learn more, visit Java Typecasting. Note that the int values are treated as ASCII values.

Does Arduino have string?

Strings are used to store text. They can be used to display text on an LCD or in the Arduino IDE Serial Monitor window. Strings are also useful for storing the user input.


1 Answers

Use like this:

String myString = String(n); 

You can find more examples here.

like image 92
Cassio Avatar answered Oct 03 '22 11:10

Cassio