Char array / string help please

I'm having a moment here. I want to do the same thing with a character array as i'm doing with a string. In the following code snip the string works but how do I get the char array to work?

String allin = "";
char  mine[20] = "";

    int z = 321;
    int x = 546;
    int y = 978;    
    allin = "";
    allin = allin + String(" ") + String(z);
    allin = allin + String(" ") + String(x);
    allin = allin + String(" ") + String(y);
    Serial.println(allin);

    strcpy(mine,z);
    strcat(mine," ");
    strcat(mine,x);
    strcat(mine," ");
    strcat(mine,y);
    Serial.println(mine);

I've tried String() and atoi() but can't get it right.

Thanks, JP

char allin[30];
char buffer[30];

int z = 321;
int x = 546;
int y = 978;

void setup()
{
    Serial.begin(115200);
    itoa(z, buffer, 10);
    strcat(allin, buffer);
    strcat(allin, " ");
    itoa(y, buffer, 10);
    strcat(allin, buffer);
    strcat(allin, " ");
    itoa(x, buffer, 10);
    strcat(allin, buffer);
    strcat(allin, " ");
    strcat(allin, atoi(x));
    strcat(allin, " ");
    strcat(allin, atoi(y));
    strcat(allin, " ");
    Serial.println(allin);

    //or easier

    sprintf(buffer, "%d %d %d", z, y, x);
    Serial.println(buffer);
}

void loop()
{
}

If you just want to print z, x and y then this works:

Serial.print(z);
Serial.print(x);
Serial.println(y);

OK, that works.

I was not using the atoi properly.

Thanks again,
JP.

The atoi() function converts a C style string to an integer. Do you perhaps mean the itoa() function that does the reverse ?

Not that by using the sprintf() function no extra conversion is needed. Some Arduino boards support an even easier way to print multiple values using a single function.

Which board are you using ?

Oops, yes itoa().

You can avoid the use of a separate buffer and strcat() for itoa by giving itoa() a pointer to the end of the c-string.

  itoa(z, allin, 10);
  strcat(allin, " ");
  itoa(y, allin + strlen(allin), 10);
  strcat(allin, " ");
  itoa(x, allin + strlen(allin), 10);
  Serial.println(allin);

Perry, thanks for getting me going again.

David, I will try that too.

My goal is to learn how to use character arrays. I ran into some of the inevitable (so they say) problems of using Strings while porting some code from a Mega to an ESP32. So, my move away from Strings has begun.

Good Day,
JP.

If you are using an ESP32 then you can do this all in a single statement

    Serial.printf("%d %d %d", z, y, x);

That is a bit odd, since the Mega with its much smaller memory would normally be the first to show noticeable problems with using String.

Would that work via wifi / lan? Like:

client.printf("%d %d %d", z, y, x);

What I am actually doing (hopefully) is sending space separated three digit integers via my lan to my data base server.

P.S.: David, there may have been other issues, I will soon find out. However, it was what prompted me to use better (?) coding practises.

I believe that it should work

Not quite, the integer will only be as many digits as needed (10 will display as "10", not "010"). You will get the same variable-length numbers using atoi() or String.

For a fixed number of digits you need to specify leading 0's and 3 digits:

  client.printf("%03d %03d %03d", z, y, x);

What caused the Esp to crash was code like this:

String allin = "";
...
    allin = "";
    allin = allin + " " + tempds1i;
    allin = allin + " " + tempds2i;
    allin = allin + " " + tempds3i;
    allin = allin + " " + tempds4i;
    allin = allin + " " + tempds5i;
    allin = allin + " " + tempds6i;
    allin = allin + " " + tempds7i;
    allin = allin + " " + tempds8i;
    allin = allin + " " + tempds9i;
    allin = allin + " " + tempds10i;

    char charBuf[4000];
    allin.toCharArray(charBuf, 500);
    client.print(charBuf);

It would compile and load but crashed the esp. This code has worked for many years on various Arduinos.

With the attention I can give it just now, it does seem odd that you would run into problems moving in the direction you did.

So it makes me request that you post as little code as you can write, in the form of a complete we can upload and try it ourselves sketch, that crashes the Esp.

Without context, it is not possible from out here to conclude that you have grabbed the right tiger by the tail.

TIA

a7

The one thing I can think of, from that particular code, is if the text is anywhere near 500 characters long and the baud rate is particularly slow the watchdog timer may be timing out. That really would not make sense anyway, since client on an ESP32 shouldn't be going through a serial port.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.