convert and print an int to char into string

Hello all,
I search in the web but I didn’t find anything about that I want to do.

I want to replace a char into a string with a integer.
e.g.
"MOD_FLT Occurs X times and Reseted "
for example to replace X with a number
I wrote this part of code, but I am sure that something is wrong because I did understand something well.

the code:

String mod_fault_message = "MOD_FLT Occurs X times and Reseted ";
void setup() {
Serial.begin(9600);
Serial.println(mod_fault_message);
for (int i=0; i<3; i++){
mod_fault_message[15]= char(i);
Serial.println(mod_fault_message[15]);
Serial.println(mod_fault_message);
delay(1000);
}
}

void loop() {
  // put your main code here, to run repeatedly:

}

and the result is attached in the picture.

I know that maybe a solution is something like this

if (i==1)mod_fault_message[15]= '1';
if (i==2)mod_fault_message[15]= '2';
.
.
.

but is there any other more sophisticated solution.

Thanks in advance

Capture.JPG

String.Replace ??

Why replace anything ?

Serial.print("MOD_FLT Occurs ");
Serial.print(aVariable);
Serial.println(" times and Reseted "):

or sprintf/snprintf:

void setup() 
{
  Serial.begin(9600);
  char mod_fault_message[64] = "";
  for (int i = 0; i < 3; i++) 
  {
    sprintf(mod_fault_message, "MOD_FLT Occurs %d times and Reseted", i);
    Serial.println(mod_fault_message);
    delay(1000);
  }
  
  //safer version... it won't write longer than the size of the buffer...
  for (int i = 0; i < 3; i++) 
  {
    snprintf(mod_fault_message, sizeof(mod_fault_message), "MOD_FLT Occurs %d times and Reseted", i);
    Serial.println(mod_fault_message);
    delay(1000);
  }
}

void loop() 
{

}

UKHeliBob actually this is a test code. Message is going to be printed in lcd screen and there will be multiple messages like this. Thanks a lot I thing that snprintf and sprintf is my solution...!!! I will try it...!!!

Message is going to be printed in lcd screen and there will be multiple messages like this.

So make the code into a function and call it with appropriate parameters when needed and treat it as an extended version of the print function.

Thanks a lot I thing that snprintf and sprintf is my solution...!!!

By all means try it but you will be using a memory hungry function that needs just as many parameters as my suggested solution. Don't plan on printing any floats either, if that matters.

You can also use i+'0' to turn an integer into a char. so your if chain becomes: mod_fault_message[15]= i+'0';

as long as i is less than 9.

UKHeliBob: By all means try it but you will be using a memory hungry function that needs just as many parameters as my suggested solution. Don't plan on printing any floats either, if that matters.

Taking you ball and going home? Why so testy?

BulldogLowell: Taking you ball and going home? Why so testy?

Testy ? Moi ?

I just think that using sprintf() or snprintf() is a sledgehammer to crack a nut, although it will work. At least those functions will get the OP away from using Strings.

UKHeliBob: I just think that using sprintf() or snprintf() is a sledgehammer to crack a nut, although it will work.

it will be well cracked!

I'm now teaching this to kids and show this method first. Folks just seem to need see how to construct a string buffer and sprintf is a good tool with minimal code. They sometimes struggle with the cryptic C format specifiers (and the cursed float issue, which should just be implemented, dammit) but once it takes, it takes.

By the time they get to the point where they are actively managing memory, they know about this tool's issues well enough to find a way to sort it all out on their own.

BulldogLowell: it will be well cracked!

I'm now teaching this to kids and show this method first. Folks just seem to need see how to construct a string buffer and sprintf is a good tool with minimal code. They sometimes struggle with the cryptic C format specifiers (and the cursed float issue, which should just be implemented, dammit) but once it takes, it takes.

By the time they get to the point where they are actively managing memory, they know about this tool's issues well enough to find a way to sort it all out on their own.

I'm with Bob on this. The String class is bloated and overweight but you prefer the easy way out. If they are struggling with the "cryptic C format", perhaps they should go with an RP and some dialect of Basic. Asking them to learn an inefficient way, then unlearn it for the right way, seems like a lot of effort. Perhaps a little more thought on how the syntax works would make it appear less cryptic and they could then learn the more efficient way to use the language from the start.

econjack: I'm with Bob on this. The String class is bloated and overweight but you prefer the easy way out. If they are struggling with the "cryptic C format", perhaps they should go with an RP and some dialect of Basic. Asking them to learn an inefficient way, then unlearn it for the right way, seems like a lot of effort. Perhaps a little more thought on how the syntax works would make it appear less cryptic and they could then learn the more efficient way to use the language from the start.

my point is that the "inefficient way" doesn't affect what they are doing, it works and is easily learned. Plus, they don't have to "unlearn" (interesting choice of words there) anything, rather... they just need to learn more.

I have no idea what RP is, or how to teach the Arduino IDE in Basic.

I guess our method is the same as learning to ride a bike, we don't teach them the most efficient way at the beginning. We focus on getting rolling and back on the bike when they fall.

Learning is a wonderful thing... The more you know, the more you need to know. The more you want to know, the more you may!

RP is a common abbreviation for Raspberry Pi and I believe they do have a dialect of Basic for it. It was designed for teaching kids about programming and to bring a fairly powerful computer into homes in Britain at a reasonable cost.

If you get the student used to working with character arrays from the start, they will have no more difficulty with that than they do learning the String class. Your code takes 37087 bytes of code space, while this version:

void setup() 
{
  Serial.begin(9600);
  char mod_fault_message[64] = "";
  char numericValue[6];
  
  for (int i = 0; i < 3; i++) 
  {
    strcpy(mod_fault_message, "MOD_FLT Occurs ");     // Copy  first part of string
    itoa(i, numericValue, DEC);                       // convert i to string (could use i + '0')
    strcat(mod_fault_message, numericValue);          // Tack onto existing string
    strcat(mod_fault_message, " times and Reseted");  // Add on last part
    Serial.println(mod_fault_message);
    delay(1000);
  }
  
  //safer version... it won't write longer than the size of the buffer...
  for (int i = 0; i < 3; i++) 
  {
//    snprintf(mod_fault_message, sizeof(mod_fault_message), "MOD_FLT Occurs %d times and Reseted", i);
    strncpy(mod_fault_message, "MOD_FLT Occurs ", sizeof(mod_fault_message));     
    itoa(i, numericValue, DEC);                       // convert i to string (could use i + '0')
    strncat(mod_fault_message, numericValue, sizeof(mod_fault_message));         
    strncat(mod_fault_message, " times and Reseted", sizeof(mod_fault_message));  
    Serial.println(mod_fault_message);
    delay(1000);
  }
}

void loop() 
{
}

takes 2380 bytes. To me, reducing code size by over 35% is worth learning.

econjack:
RP is a common abbreviation for Raspberry Pi and I believe they do have a dialect of Basic for it. It was designed for teaching kids about programming and to bring a fairly powerful computer into homes in Britain at a reasonable cost.

If you get the student used to working with character arrays from the start, they will have no more difficulty with that than they do learning the String class. Your code takes 37087 bytes of code space, while this version:

void setup() 

{
  Serial.begin(9600);
  char mod_fault_message[64] = “”;
  char numericValue[6];
 
  for (int i = 0; i < 3; i++)
  {
    strcpy(mod_fault_message, "MOD_FLT Occurs “);    // Copy  first part of string
    itoa(i, numericValue, DEC);                      // convert i to string (could use i + ‘0’)
    strcat(mod_fault_message, numericValue);          // Tack onto existing string
    strcat(mod_fault_message, " times and Reseted”);  // Add on last part
    Serial.println(mod_fault_message);
    delay(1000);
  }
 
  //safer version… it won’t write longer than the size of the buffer…
  for (int i = 0; i < 3; i++)
  {
//    snprintf(mod_fault_message, sizeof(mod_fault_message), “MOD_FLT Occurs %d times and Reseted”, i);
    strncpy(mod_fault_message, "MOD_FLT Occurs “, sizeof(mod_fault_message));   
    itoa(i, numericValue, DEC);                      // convert i to string (could use i + ‘0’)
    strncat(mod_fault_message, numericValue, sizeof(mod_fault_message));       
    strncat(mod_fault_message, " times and Reseted”, sizeof(mod_fault_message)); 
    Serial.println(mod_fault_message);
    delay(1000);
  }
}

void loop()
{
}




takes 2380 bytes. To me, reducing code size by over 35% is worth learning.

agreed, and not at all meant to be argumentative, getting 11 year old kids to do their cool stuff faster is more important than saving the 35%, at the beginning.

:wink:

PS, here, we just call it Pi, thanks!

BulldogLowell: ...getting 11 year old kids to do their cool stuff faster is more important than saving the 35%, at the beginning.

PS, here, we just call it Pi, thanks!

Whatever works, but I do hope you'll at least mention the alternative.

As to RP versus Pi, you know us colonials...always looking for a different way...