0
votes

actually I have a string from this type :

"1,Hello,E025FFDA,-126.56,52.34,true"

And I want to parse it using sscanf to store those value in variable of corresponding type so

uint, char[], maybe string here, ufloat ?, float, bool

I use sscanf with "%[^',']" format specifier and I test each value if it is correct or not and in a certain range.

Here is my code :

  • param is the pointer to the beginning of the string

  • temp2 a temporary variable

  • local_ptr is a copy of param to not modify param

  • RCV a struct with all my parameters

      if((get_param_length(param) != 0)
      && (sscanf(local_ptr, "%[^',']", &temp2) == 1))
    

    { RCV.binarypayload = temp2; } else { error = 1; }

Do you have any idea of the best type to store my values ? Or maybe advices ?

1
A negative float is just float - ForceBru
float numbers are best stored in 'float' or 'double' types - 0___________
What is %[^','] supposed to do? Do you mean %[^,]? - Gerhardh

1 Answers

0
votes

There is no type that could match your ufloat. All floating point types are signed so extracting from your string could be done like this:

#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>

int main() {
    unsigned u1, u2;
    char str1[20], boolstr[6];
    float f1, f2;
    const char *local_ptr = "1,Hello,E025FFDA,-126.56,52.34,true";
    bool b;

    if(sscanf(local_ptr, 
       " %u,%19[^,],%X,%f,%f,%5s", &u1, str1, &u2, &f1, &f2, boolstr) == 6)
    {
        b = strcmp(boolstr, "true") == 0;
        printf("%u %s %X %f %f %d\n", u1, str1, u2, f1, f2, b);
    }
}

Possible output:

1 Hello E025FFDA -126.559998 52.340000 1