Pages: [1]   Go Down
Author Topic: Concatenate two char arrays (for udp client)  (Read 3456 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 415
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm using Arduino as a UDP client to send information about values read from sensors.

I'd like to send packets like:

"string1=value1"

where value1 is an integer read from Arduino analog pins.
The problem is that udp.write() only takes as parameter char arrays, so i can't sum a string to an integer and then pass the new string to udp.write().

I have to concatenate a char array to an integer value and then pass the array to the udp.write() function.

At the moment, i'm using
Code:
dtostrf(FrontSonar_Reading,1,2,temp);
in order to save the integer value in the "temp" array char, but i do not know how to concatenate this result to the array char.

Actually, I have something like:

Code:
char string1[] = "string1=";

void loop(){
  //...
  dtostrf(FrontSonar_Reading,1,2,temp);
  Udp.beginPacket(remoteIp, remotePort);

   Udp.write(string1);
   Udp.write(temp);

   Udp.endPacket();
}

it works, but it sends two packets; i'd like to send just one packet with the string and the integer at the same time.

Is it possible?
Logged

Belgium
Offline Offline
Edison Member
*
Karma: 68
Posts: 1926
Arduino rocks; but with my plugin it can fly rocking the world ;-)
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm not sure this is the best way to do it when you use UDP but sprintf works in all cases.
Below is a sketch that concatenates a string "=" a value.
Code:
void setup() {
  Serial.begin(115200);
}

void loop() {

    char myConcatenation[80];
    char myCharArray[16]="A variable name";
    int myInt=5;
    sprintf(myConcatenation,"%s = %i",myCharArray,myInt);
    Serial.println(myConcatenation);
    delay(100);
}
look at this link for more info on sprintf http://www.cplusplus.com/reference/clibrary/cstdio/sprintf/
I hope this helps
Best regards
Jantje
Logged

Do not PM me a question unless you are prepared to pay for consultancy.
Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 415
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks a lot for your suggestion!

Is sprintf() safe? Can it have memory lacks or problems?

I'd like to build a roboust application and i'm trying to avoid memory problems.

What do you think about this?

Thanks again.
Logged

0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 415
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Can this function be more safer than sprintf()?

Code:
char * BufferInsertStringValue(char * s,int startingPosition, char * command) {
  int i = 0;
  int stringLength=strlen(s)-1;
  for ( i = 0;i <= stringLength;i++) {
    command[startingPosition + i - 1] = s[i];
     }
  command[startingPosition + i] = '\0';
  return command;
}

where s is the temp array char and comando is the original text string which i want to concatenate with the integer value.
I call this function with:
Code:
char * vett;
vett = BufferInsertStringValue(temp, 14, sonar_center);

Do you think it's safer than sprintf function?
Logged

Belgium
Offline Offline
Edison Member
*
Karma: 68
Posts: 1926
Arduino rocks; but with my plugin it can fly rocking the world ;-)
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The only risk you have with sprintf is that you will outrun your target buffer space.
As you are using a char buffer containing the variable name and you are adding a int you can 100% guarantee your buffer is big enough.
Unless you go for runtime memory allocation (like String class does) you will always have this problem.
Best regards
Jantje
Logged

Do not PM me a question unless you are prepared to pay for consultancy.
Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

Offline Offline
Jr. Member
**
Karma: 0
Posts: 83
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello Friends,  I am exploring the below sketch to concatenate  arrays..  If I want to add "%" symbol to the end of the integer value to myConcatenation[80] array, how do I do it?  basically, when I print myConcatenation, I would like to see the output as "A variable name = 5 %.

Any guidance is appreciated.

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

void loop() {

    char myConcatenation[80];
    char myCharArray[16]="A variable name";
    int myInt=5;
    sprintf(myConcatenation,"%s = %i",myCharArray,myInt);
    Serial.println(myConcatenation);
    delay(100);
}
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 83
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok.  Figured it out myself with below code.  Is there any other better way to achieve it?

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

void loop() {

    char myConcatenation[80];
    char myCharArray[]="A variable name";
    int myInt=5;
    char myStr[] = "%";
    sprintf(myConcatenation,"%s = %i %s",myCharArray,myInt,myStr);
    Serial.println(myConcatenation);
    delay(100);
}
Logged

Belgium
Offline Offline
Edison Member
*
Karma: 68
Posts: 1926
Arduino rocks; but with my plugin it can fly rocking the world ;-)
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Is there any other better way to achieve it?
Reading the sprintf documentation?  smiley-twist
You probably need to add a \ before the % but I'm sure google can point you to documentation of the printf family that tells you how to do it.
Logged

Do not PM me a question unless you are prepared to pay for consultancy.
Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

Offline Offline
Jr. Member
**
Karma: 0
Posts: 83
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for your direction.  Got the answer from the printf statement guide.  Below one does the trick.

Code:
sprintf(myConcatenation,"%s = %i%%",myCharArray,myInt)
Logged

Belgium
Offline Offline
Edison Member
*
Karma: 68
Posts: 1926
Arduino rocks; but with my plugin it can fly rocking the world ;-)
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

great :-)
So % is the escape character and not \. Makes sense.
Best regards
Jantje
Logged

Do not PM me a question unless you are prepared to pay for consultancy.
Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

Pages: [1]   Go Up
Jump to: