[Solved]Converting a string to an array

Hello community,

I am tinkering with the 433MHz rf module & virtual wire library. I am following this tutorial. ControlRobotics: Tutorial: Arduino + Kit rf 433Mhz (Part4: Multiple variables)
Here, in the Tx sketch all the 4 input parameters are added to a string using this command

sprintf(Sensor1CharMsg,
"%d,%d,%d,%d,"
, Sensor1Data, Sensor2Data, Sensor3Data, Sensor4Data);

I understans that there is 4 decimal numbers so 4 %d
But in the Rx sketch, there are 6 %d for seperating the data

sscanf(StringReceived, "%d,%d,%d,%d,%d,%d",&Sensor1Data, &Sensor2Data,&SensorData3,&SensorData4); // Converts a string to an array

I want to know that if it is a typo or there is any reason to use 6 %d for seperatong 4 decimal values?

Thanking you in anticipation.

I want to know that if it is a typo or there is any reason to use 6 %d for seperatong 4 decimal values?

It's a mistake. There should be one format specifier for each variable listed.

Thanks Paul for the clarification.
Cheers.

One more thing to know, in the above strings how would the RX end know that how many characters are to be separated to get 4 separate values? they are in an array so how come the RX know the length of each of the 4 fragments of the received string?

nightcrawler218:
One more thing to know, in the above strings how would the RX end know that how many characters are to be separated to get 4 separate values? they are in an array so how come the RX know the length of each of the 4 fragments of the received string?

The data is formatted by the sprintf() statement, and the resultant String (note the upper case 'S', it's an object that encapsulates a char array) is populated by the sscanf() statement.

Assuming the 4 values sent across are

  • Sensor1Data = 123
  • Sensor2Data = 45
  • Sensor3Data = 768
  • Sensor4Data = 93
    The string (note the lower case 's'. it's an array of char) sent is "123,45,768,93," Note that the trailing comma is unnecessary.
    The receiver receives the string, and the sscanf() knows the format, and converts the string to a String. When you correct the number of variables being sent and received, you should make the receiver's format string the same as the sender's. Either leave off the trailing comma in both, or leave it in both.

lar3ry:
The string (note the lower case 's'. it's an array of char) sent is "123,45,768,93," Note that the trailing comma is unnecessary.

You mean thisprintf(Sensor1CharMsg, "%d%d%d%d" , Sensor1Data, Sensor2Data, Sensor3Data, Sensor4Data);
instead of that?printf(Sensor1CharMsg, "%d,%d,%d,%d," , Sensor1Data, Sensor2Data, Sensor3Data, Sensor4Data);

In the RX node
In the first case what would be the terminating character that denotes end of 1data, 2data etc?
In the second case are the trailing commas terminating character?

You mean this…instead of that

Did you miss the part about the TRAILING comma?

ohhow. right I forgot to delete the commas. it will be printf(Sensor1CharMsg, "%d%d%d%d" , Sensor1DataSensor2DataSensor3DataSensor4Data);

No, that won’t work - think about it.

printf(Sensor1CharMsg, “%d,%d,%d,%d,” , Sensor1Data, Sensor2Data, Sensor3Data, Sensor4Data);
You only needed to remove the trailing comma.

I forgot the "s" ?

No. Suppose that the values are 11, 22, 33, and 44. The statement you have will create a buffer containing "11223344". How are you going to then parse that? You have no idea where one value ends and the next begins.

Thanks Sir.
My bad. I misunderstood the part with trailing commas.

PaulS:

I forgot the "s" ?

No. Suppose that the values are 11, 22, 33, and 44. The statement you have will create a buffer containing "11223344". How are you going to then parse that? You have no idea where one value ends and the next begins.

It means in Tx node I have to put a comma in between 2 separate values like

sprintf(Sensor1CharMsg, "%d,%d,%d,%d" , Sensor1Data, Sensor2Data, Sensor3Data, Sensor4Data);

and in the Rx mode the string will be cut down into pieces everytime it sees a comma inbetween the values ri8?

sscanf(StringReceived, "%d,%d,%d,%d",&Sensor1Data, &Sensor2Data,&SensorData3,&SensorData4);

It means in Tx node I have to put a comma in between 2 separate values like...and in the Rx mode the string will be cut down into pieces everytime it sees a comma inbetween the values ri8?

Yes.

Thanks a ton...
:slight_smile:

Hello everyone,

Today I was going through the tiny GPS library test_with_gps_devices example.
Need to clear a couple of doubts.

sprintf(sz, "%02d/%02d/%02d %02d:%02d:%02d ", month, day, year, hour, minute, second);

Why it cant be

sprintf(sz, "%02d/%02d/%02d %02d:%02d:%02d ", month/day/year hour:minute:second);

Declaration static void print_float(float val, float invalid, int len, int prec)
call   print_float(flat, TinyGPS::GPS_INVALID_F_ANGLE, 10, 6);
What is the last argument "prec" ? & what is that double colon thing "TinyGPS::GPS_INVALID_F_ANGLE"?
Now arduino seems bit harder to understand :-/

Why it cant be

Because that is syntactically invalid. You are not understanding the formatting part, between the double quotes, and the list of variables containing values to be formatted.

What is the last argument "prec" ?

Because that makes more sense than calling the precision value waterBuffalo.

& what is that double colon thing

It's called a scope resolution operator. It tells the compiler the scope that the variable/value is defined in (TinyGPS in that case).

Now arduino seems bit harder to understand

Harder than what?

Now arduino seems bit harder to understand :-/

That's not Arduino, that's C++.