Trying to add 2 floats an int and the char '-' to char array

Hello, I am trying to add 2 floats, an integer, and a single charachter -, to an char array. But so far it is not working with different functions.
Would anyone be nice enough to explain what is going wrong?

while(){
randominteger = 10;

char testarray[32];
char tmp[2] = {'-'};
itoa(randominteger,testarray,10);
strcpy(testarray,tmp);
dtostrf(ss.getLat(),8,6,testarray);
strcpy(testarray,tmp);
dtostrf(ss.getLng(),7,6,testarray);
Serial.print(testarray);

randominteger = 0;

Serial.print(testarray);
}

ss.getLat = 52.283199 and ss.getLng = 4.851055
The result what i am getting is this: 4.85114252.283130 ( lng is first then lat but no - or the integer value from randominteger=10;)

These two lines:

itoa(randominteger,testarray,10);       // Generate ascii for number and place in testarray[]
strcpy(testarray,tmp);                        // Overwrite testarray with tmp

Your code trashes testarray[] with the second line above.

Walorda:
Hello, I am trying to add 2 floats, an integer, and a single charachter -, to an char array.

If the purpose is just to send a sequence of characters with Serial there is no need to build them into an array. Just send them with separate Serial.print() commands. The receiving device won't know the difference.

...R

Robin2:
If the purpose is just to send a sequence of characters with Serial there is no need to build them into an array. Just send them with separate Serial.print() commands. The receiving device won't know the difference.

...R

Hai, i am trying to send it with the rf24 library to my raspberry so it can get the values separated by the - character. it just seemed like an easy way to do but clearly i was wrong -__-

try my CStringBuilder library. install it with Library Manager

strcpy(x,y) replaces x with y. If you are trying to build on ling character array of sequential items then use strcat(x,y) char x[] will be appended with y[].

You didn't list the expected result, so I'm guessing from the information in the OP.

You expect to have
52.2831994.85105510-

as your output
That would be:
dtostrf(ss.getLat(),8,6,tmp);
strcat(testarray,tmp);
dtostrf(ss.getLng(),7,6,tmp);
strcat(testarray,tmp);
itoa(randominteger,tmp,10);
strcat(testarray,tmp);
strcat(testarray,"-");
Serial.print(testarray);

Or like Robin2 said just Serial.print() each item individually. The pi will receive the same data.

Thanks for the help guys it now works :slight_smile:

Walorda:
Hai, i am trying to send it with the rf24 library to my raspberry so it can get the values separated by the - character. it just seemed like an easy way to do but clearly i was wrong -__-

For the RF24 I would put the data into an array or a struct

...R
Simple nRF24L01+ Tutorial