-2

I want to do something similar to this:

char* a = (char*)msg[0];
char* b = (char*)msg[1];
char* c = a + "," + b;

Where msg is an array of int.

N.B.: This is Arduino C++, not regular C++.

Rapptz
  • 20,807
  • 5
  • 72
  • 86
Jongz Puangput
  • 5,527
  • 10
  • 58
  • 96

3 Answers3

2

Arduino doesn't use std::string, instead it uses String (note the capital S and dropped std::). They're used the same way as std::string for the most part. So basically you should just be able to do this:

String a("hello");
String b(" world");
c = a + b;

If you want to convert an integer to a String, it has a constructor to do just that, e.g.:

String a = String(msg[0]);
String b = String(msg[1]);

You can find more examples here and here.

Rapptz
  • 20,807
  • 5
  • 72
  • 86
1

See strcat. You seem to be programming C, not C++. This should be covered in the most basic tutorials.

Daniel Albuschat
  • 806
  • 8
  • 20
  • am I misunderstand?? don't arduino is develop in c++?? – Jongz Puangput Jul 29 '14 at 09:52
  • @JongzPuangput Usually, when you write code that looks like C, people say "this is not C++". But technically, a lot of C code is also valid C++. Not sure yours is, because I don't know what `msg` is, but the cast to `char*` is likely to be wrong. – juanchopanza Jul 29 '14 at 09:55
  • 1
    @JongzPuangput That doesn't mean it is not wrong to cast. But there is not enough information to know for sure. – juanchopanza Jul 29 '14 at 09:58
1

SOLUTION

so here is my solution thank everyone.

  String a = String(msg[0]);
  String b = String(msg[1]);
  String c = a + "," + b;
  char* d;
  c.toCharArray(d,c.length());

  mclient.publish("topic1/sensorAck",d);
Jongz Puangput
  • 5,527
  • 10
  • 58
  • 96