How to convert int to string on Arduino?

How to convert int, n to a string so that when sending it by serial number it is sent as a string?

This is what I still have:

 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++; } 
+58
arduino
Oct 26 2018-11-11T00:
source share
7 answers

Use this:

 String myString = String(n); 

Below you can find examples.

+84
Oct 27 '11 at 11:03
source share

You can simply do:

 Serial.println(n); 

which automatically converts n to an ASCII string. See the documentation for Serial.println() .

+18
Oct 27 '11 at 16:25
source share

use the itoa() function included in stdlib.h

 char buffer[7]; //the ASCII of the integer will be stored in this char array itoa(-31596,buffer,10); //(integer, yourBuffer, base) 
+14
Jan 02 '14 at 20:15
source share

This is a speed-optimized solution for converting int (16-bit integer) to string.

This implementation avoids the use of division, since the 8-bit AVR used for Arduino does not have a hardware DIV command, the compiler translates division into lengthy repeated subtractions. Thus, the fastest solution uses conditional branches to build the string.

Fixed buffer with 7 bytes received from the beginning in RAM to avoid dynamic allocation. Since this is only 7 bytes, the cost of using fixed memory is considered minimal. To help the compiler, we add a case modifier to the variable declaration to speed up execution.

 char _int2str[7]; char* int2str( register int i ) { register unsigned char L = 1; register char c; register boolean m = false; register char b; // lower-byte of i // negative if ( i < 0 ) { _int2str[ 0 ] = '-'; i = -i; } else L = 0; // ten-thousands if( i > 9999 ) { c = i < 20000 ? 1 : i < 30000 ? 2 : 3; _int2str[ L++ ] = c + 48; i -= c * 10000; m = true; } // thousands if( i > 999 ) { c = i < 5000 ? ( i < 3000 ? ( i < 2000 ? 1 : 2 ) : i < 4000 ? 3 : 4 ) : i < 8000 ? ( i < 6000 ? 5 : i < 7000 ? 6 : 7 ) : i < 9000 ? 8 : 9; _int2str[ L++ ] = c + 48; i -= c * 1000; m = true; } else if( m ) _int2str[ L++ ] = '0'; // hundreds if( i > 99 ) { c = i < 500 ? ( i < 300 ? ( i < 200 ? 1 : 2 ) : i < 400 ? 3 : 4 ) : i < 800 ? ( i < 600 ? 5 : i < 700 ? 6 : 7 ) : i < 900 ? 8 : 9; _int2str[ L++ ] = c + 48; i -= c * 100; m = true; } else if( m ) _int2str[ L++ ] = '0'; // decades (check on lower byte to optimize code) b = char( i ); if( b > 9 ) { c = b < 50 ? ( b < 30 ? ( b < 20 ? 1 : 2 ) : b < 40 ? 3 : 4 ) : b < 80 ? ( i < 60 ? 5 : i < 70 ? 6 : 7 ) : i < 90 ? 8 : 9; _int2str[ L++ ] = c + 48; b -= c * 10; m = true; } else if( m ) _int2str[ L++ ] = '0'; // last digit _int2str[ L++ ] = b + 48; // null terminator _int2str[ L ] = 0; return _int2str; } // Usage example: int i = -12345; char* s; void setup() { s = int2str( i ); } void loop() {} 

This sketch was compiled into 1,082 bytes of code using avr-gcc, which is bundled with Arduino v1.0.5 (the size of the int2str function itself is 594 bytes). Compared to a solution using a String object that is compiled into 2.398 bytes, this implementation can reduce your code size by 1.2 Kb (it is assumed that you do not need another method of the String object, and your number strictly corresponds to the type of the signed type).

This function can be optimized further by writing it with the appropriate assembler code.

+9
Dec 13 '13 at 13:30
source share

You just need to wrap it around a String object as follows:

 String numberString = String(n); 

You can also do:

 String stringOne = "Hello String"; // using a constant String String stringOne = String('a'); // converting a constant char into a String String stringTwo = String("This is a string"); // converting a constant string into a String object String stringOne = String(stringTwo + " with more"); // concatenating two strings String stringOne = String(13); // using a constant integer String stringOne = String(analogRead(0), DEC); // using an int and a base String stringOne = String(45, HEX); // using an int and a base (hexadecimal) String stringOne = String(255, BIN); // using an int and a base (binary) String stringOne = String(millis(), DEC); // using a long and a base 
+8
Aug 11 '14 at 23:08
source share

The solution is too great. Try this simple one. Please provide a 7+ character buffer, no checks.

 char *i2str(int i, char *buf){ byte l=0; if(i<0) buf[l++]='-'; boolean leadingZ=true; for(int div=10000, mod=0; div>0; div/=10){ mod=i%div; i/=div; if(!leadingZ || i!=0){ leadingZ=false; buf[l++]=i+'0'; } i=mod; } buf[l]=0; return buf; } 

It can be easily modified to return the end of the buffer if you drop the "l" index and immediately increase the buffer.

+1
Dec 14 '13 at 9:26
source share

the first example uses the classic string C: it is just an array of characters ending in zero (the NUL character). other examples use the String class, the C ++ class, which I would give away, except for examples of toys and tiny demos. It leaks memory in ways that are not so easy to predict, and leads to mysterious board locks.

-2
Oct 17 '12 at 15:17
source share



All Articles