Arduino String(), convert int to String

I am having trouble converting an int to a String. I am coding a PH7 and i have included Arduino
but it is not working. Everything i have read indicates that it should work. Can someone please help me here.

#include <Arduino.h>

int i = 5;
String printChar = String(i);

when i run this printChar is empty??? Is there a better way.

Works perfectly fine in a simulated ESP32 environment, so I assume it’ll work the same on ArduinoCore STM32.

The code exactl you’ve shown is not complete. Can you send a minimal, complete code?

here is a snipet

#include <Arduino.h>
#include <Arduino_PortentaBreakout.h>
#include "ProjConfig.h"
#include <LiquidCrystal_I2C.h>
#include <ArduinoBLE.h>
#include "MyBT.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// set the display to automatically scroll:
    // print from 0 to 9:
    for (int thisChar = 0; thisChar < 10; thisChar++) {
      //string printChar = __itoa(thisChar,);
      char buffer[16];
      sprintf(buffer, "%d", thisChar);
      String printChar = String(buffer);
      //String printChar = String(_itoa(thisChar));
      lcdPrint(printChar, NO, 0,0);

function lcdPrint is in ProjConfig.cpp .

debug gives me this error -var-create: unable to create variable object (from var-create watch_4c2a8fe7eaf24721cc7a9f0175115bd4} @ "Message")

Note i have tried String(int) method and sprinf method, but both dont work.

And when you call lcdPrint("0", NO, 0,0); manually it does print it?

Yes i have sent all types of alpha numerical to lcdPrint. In the snippet the problem happens before i call icdPrint.

The annoying thing is a can see the int “0” when i hover over printChar , its there just can extract it.

{static FLT_MAX_DECIMAL_PLACES = 10, static DBL_MAX_DECIMAL_PLACES = 10, buffer = 0x240155a0 "0", capacity = 1, len = 1}

You need to eliminate whether the string generation is wrong or something weird happens inside the LCD update.

Write this minimal sketch

#include <Arduino.h>
void setup() { Serial.begin(115200); }

void loop() {
    for (int thisChar = 0; thisChar < 10; thisChar++) {
      char buffer[16];
      sprintf(buffer, "%d", thisChar);
      String printChar = String(buffer);

Does it print 0 to 9 repeadedly? If yes, string conversion is not the issue.

Yes that works. hmmm, where to next? i will put a serial.println in my code

I can send converted int to Serial.println Serial.println(printChar);

Ok thanks, it is working now, i turned off the scroll and confirmed it will atleast send the int to the LCD. ill work on the scoll function now.
Thansk again for your work.

Hi i got this to work but theis make me curious. In the debugger i put a watch on the variable printChar as well as others. the others were updating but not printChar. This is what mislead me.

Can you show in screenshots what it looked like?

I think because printChar is a String (Arduino created struc, correct?) then the value is actually in the buffer, in expanded view. Where as thisChar is an int so it is shown top view.



After String printChar = String(thisChar);

Yes since String is a class created in the Arduino framework, in the debug view it’ll only show its updated contents in the expanded view. There’s no further decoding done for it to directly print the string value next to the variable name (but for other types such as std::string containers this is done).

Thanks. These leads me to 2 questions.
1, Is there any prefference to which to use String or std::string?
2, Note in my code i am defining printChar within the for loop. String printChar = String(thisChar). Would it be better practice to define befor the for loop and simple update ie printChar = String(thisChar) ?


In Arduino world you usually use String. There may also be some Arduino cores with compilers that don’t actually have the C++ STL (or std::string).

Yes, you can avoid a destructor call this way.

The more efficient way would be to throw away String, to only once allocate a buffer before the loop and itoa() in every loop operation. The LCD update function should then accept a const char* instead of a String, or at least provide a function overload so that both are accepted. That would eliminate the heap usage for this piece of code.

Mandatory lecture:

Thanks i will do the lecture. also what include do i need for itoa()? ive tried to get to work and i cant invoke it.



it’s in #include <stdlib.h> (or #include <cstdlib> as the C+±ified version of that).

ive tried <stdlib> , ill give <cstdlib> ago .

Now im haveing trouble with my passing char’s as well as the itoa function.

I have include #include <cstdlib> but the itoa function has the squiggly lines with ‘identifier “itoa” is unddfined’. But the code all works fine.

Why squiggly line and a red problem but it works ok?

I have worked thru the tute you sent the link to, very helpful. I think my char formatting is correct. is it the itoa function?