Make a cstring from multiple variables

Hi all,

I want to make a cstring equal variable1 and variable2 and varaible3.

So if:

Variable1 = 1
Variable2 = 2
Variable3 = 3

the result is 123.

My question is how can I do this? Can I modify the strcpy command to achieve this?

Any help with this would be awesome!

Thanks!

Zeb

  char buffer[20];
  int total = (100 * variable1) + (10 * variable2) + variable3;
  itoa(total, buffer, 10);
  Serial.println(buffer);

Why do you want to do this ?

void setup() {
    Serial.begin(115200);

    int var1 = 1;
    int var2 = 2;
    int var3 = 3;

    char s [20];
    sprintf (s, "%d%d%d", var1, var2, var3);
    Serial.println (s);
}

void loop() {
}

Hi, thanks for your help!

UKHeliBob:

  char buffer[20];

int total = (100 * variable1) + (10 * variable2) + variable3;
  itoa(total, buffer, 10);
  Serial.println(buffer);




Why do you want to do this ?

Does this work when "Variable1", "Variable2" and "Variable3" are cstrings?

Thanks very much!

Zeb

Because this code:

char variable1[] = "1";
char variable2[] = "2";
char variable3[] = "3";

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  char buffer[20];
  int total = (100 * variable1) + (10 * variable2) + variable3;
  itoa(total, buffer, 10);
  Serial.println(buffer);

}

throws up this error:

invalid operands of types 'int' and 'char [2]' to binary 'operator*'

About this line:

  int total = (100 * variable1) + (10 * variable2) + variable3;

Thanks again,

Zeb

Sorry in my original post I didn't explain clearly that "Variable1", "Varaible2" and "Varaible3" are cstrings!

Sorry about that!

Zeb

All good, actually just used the strcat command:

char var1[] = "1";
char var2[] = "2";
char var3[] = "3";
char str[80];

void setup() {
  Serial.begin(115200);
}

void loop() {
  strcpy (str,var1);
  strcat (str,var2);
  strcat (str,var3);
  Serial.println (str);
}

Thanks heaps for your help!

Zeb

Does this work when "Variable1", "Variable2" and "Variable3" are cstrings?

If you wanted to concatenate C style strings then why did you not say so in the first place ?

It is so much easier to provide help when in possession of the full facts