Concating String and Integer

I am unable to concat string like following

int i = 12; Serial.println("p"+i+"p");

It is giving me an error like "invalid operands of types 'const char*' and 'const char [2]' to binary 'operator+' "

i have met this when trying to send a GET request like following

client.get("192.168.1.2/accident/script.php?vehicle_id="+vehicle_id+"&datetime="+dateTime);

here data type of vehicle_id is int

Here also the same error like above is generated

How to solve this?

How to solve this?

int i = 12;
  Serial.print("p")
  Serial.print(i);
  Serial.println("p");

You seem to be under the completely mistaken impression that + is the concatenation operator in C. It is the ADDITION operator. Grab your calculator. ADD "p" and 12. What is the result?

If your function need the string as one chunk, you could use sprintf.

const char getFmt[] PROGMEM = "192.168.1.2/accident/script.php?vehicle_id=%ld&datetime=%s";
unsigned long vehicle_id = 123456789;
char dateTime[] = "201705161540";

void setup() {
  char bigBuf[100] = "";
  Serial.begin(250000);
  sprintf_P(bigBuf, getFmt, vehicle_id, dateTime);
  Serial.print(F("we now have '"));
  Serial.print(bigBuf);
  Serial.println(F("'"));
}
void loop() {}
we now have '192.168.1.2/accident/script.php?vehicle_id=123456789&datetime=201705161540'

But remember, in most cases sprintf() is a bit of a bazooka to kill a mosquito. :wink:

If you want to have a smaller footprint, you could use

const char getFmt1[] PROGMEM = "192.168.1.2/accident/script.php?vehicle_id=";
const char getFmt2[] PROGMEM = "&datetime=";

unsigned long vehicle_id = 123456789;
char dateTime[] = "201705161540";

void setup() {
  char bigBuf[100] = "";
  Serial.begin(250000);
  strcpy_P(bigBuf, getFmt1);
  char* ptr = bigBuf + strlen(bigBuf);
  ultoa(vehicle_id, ptr, 10);
  strcat_P(bigBuf, getFmt2);
  strcat(bigBuf, dateTime);
  Serial.print(F("we now have '"));
  Serial.print(bigBuf);
  Serial.println(F("'"));
}
void loop() {}
we now have '192.168.1.2/accident/script.php?vehicle_id=123456789&datetime=201705161540'

PaulS: You seem to be under the completely mistaken impression that + is the concatenation operator in C. It is the ADDITION operator. Grab your calculator. ADD "p" and 12. What is the result?

"p" is a char, not a String (with uppercase S), + operates on char much like two values in a calculator. However,

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

void loop() {
  String p = "p";
  int i = 12;
  String c = p + i + p;
  Serial.println(c);
}

is String addition (concatenation) that does work. One can add the value of a function to a String, as long as the String has been initialized beforehand. One should concatenate Strings on a line before using Serial.print(). See 08. Strings for more information.

Perehama: "p" is a char, not a String (with uppercase S)

No, "p" is a string literal. 'p' is a char.

You could look at using the PrintEx library. That will add printf() support to Print class objects - which eliminates having to use sprintf() to buffer each time you want to output a xxprintf() formatted string.

--- bill

try adding outside the print() function

String a="p";
  a=a+12+"p";
  Serial.print(a);

Why o why. Just print each part and don't spend time to concat them in memory when you don't need it..

septillion: Why o why. Just print each part and don't spend time to concat them in memory when you don't need it..

This works only most of the times, if you have a Stream interface to feed.

If you have to pass a full URI to some function, you have to concatenate if the data is dynamic.

Yep, and he is :)

Whandall:
This works only most of the times, if you have a Stream interface to feed.

karma++

plus, there is more than one way to skin this con-cat

nothing wrong with showing OP alternatives

septillion:
Yep, and he is :slight_smile:

client.get("192.168.1.2/accident/script.php?vehicle_id="+vehicle_id+"&datetime="+dateTime);

At least not for this special function.

I replied to the one above aka to logans.

But as long it's a print statement, don't bother concating it first, just send it in pieces, nobody will know. In case of the web with a .get(), yeah, you unfortunately have to.

septillion:
Why o why. Just print each part and don’t spend time to concat them in memory when you don’t need it…

I’m a php programmer with an Arduino hobby.
C++ is total nuts.

I just want a forum that helps with total 1980 wackyness of C.

I need to make a filename for my SD reader.
And I’m searching this idiotic forum for help.

Look how other forums help:
search in google: create string with variables php

Need to waist 4 hours of time? Then try to get the same answer for Arduino on this forum.

After reading 9127 comments of idiots like Paul_S I ended up using this:

#include <stdio.h>

unsigned int selectedYear;

char filename[14] = {}; // ROMEO filenames are 8.3 so 12 + 1 for END-OF-STRING should be enough...

void createFilename(int yr) {

  sprintf(filename, "thumb%u.bmp", yr);

}



// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  
  selectedYear = 1970;
  Serial.println("Start");
}

// the loop routine runs over and over again forever:
void loop() {

  createFilename(selectedYear); 

  Serial.print("- ");
  Serial.println(filename);
  
  selectedYear++;
  if ( selectedYear == 2021 ) selectedYear = 1970;
  delay(200);

}

Making sure my format fits the values I was using: a unsigned int. I found a reference here:
http://www.cplusplus.com/reference/cstdio/printf/

Now I will probably want a folder name in the filename as well. …
So I better watch out to keep filename the right size …

After nearly three years.

bespired: C++ is total nuts.

Try to write real time stuff in PHP, Java or C#,even on the fastest PC that you can find in the world; and I mean real time stuff where microseconds can cost you thousands of dollars. You will fail miserably. Those languages are basically only good for user interfaces.