converting string to number, with a twist!

I'm a real newbie at sketch writing and programming and as such, it is difficult to search for answers as I'm not sure of the correct terminology to look for!

So.... I have a serial input that is delimited by commas and comprises alpha and numeric content, but essentially is all ASCII text - it is in fact an NMEA0183 'sentence' of data.

Example

$WIMWV,007,R,4.1,N,A*3F

I want to extract the numerical value of the text field after the first comma (007 in this example) and that after the 3rd comma (4.1 in the example)

I cannot do this by simply choosing the 'nth character as the number of characters can vary - for example the field that is 4.1 in this example, could easily be 12.5, so counting the position, by number of characters within the string, does not work.

Essentially I need a way to to extract the part of the string between two specified commas and then convert it to a number. (using toint or tofloat, I assume)

The reason I wish to do this is that this input gives wind speed and wind direction. I wish to save the maximum wind speed experienced from start of running the sketch. (adding an ability to re-set it with a suitable key switch). I also wish to output the speed and direction value to an LCD display.

But I am stuck at how to extract the two values I need, consistently, and convert it to numbers rather than text.

Assuming you are using '\0' terminated char arrays, (as you should!) check out strtok() which, while tricky to use, does exactly what you want.

Sentences like that have to follow certain rules, such as they always have the same number of commas defining the data fields.

I also suggest strtok(), but it is easy enough to go through the string character by character, counting commas, and then isolate the segments that contain the numbers you want.

Then convert those segments to integers or floats using the atoi() or atof() functions.

Thanks

I have however now found a handy library - TinyGPS++ - which includes a function to extract the nth field from any specified NMEA sentence! It can then be converted to a number as required.

But your replies are useful for any future project to do the same that isn't NMEA specific