Important: Please read the Qt Code of Conduct -

Parsing strings that contain raw and ascii

  • I know this is for C++, nevertheless I 'm asking for some hints regarding C.

    I have a string that always has the length of the next section in raw hex and then the following section as ASCII chars:

    03 53 65 63 74 31 02 31 32 ...
    3 bytes, Sect1
    2 bytes 12

    I am looking for an easy way to parse this, I need the ASCII strings or if it contains a number like the 12 above, the 12 as int.

  • Moderators

    Just to clarify the example you gave - does the file contain this as a text?
    So character 0, character 3, space... or these are actual single byte values? 3, 53, 65 ... ?

  • These are single byte values.

  • Moderators

    Ok, so the process is simple:

    fread() a byte
    allocate a buffer of size read previously
    fread() to that buffer
    use strtol() and errno() to check if it's a number
    do something with result*
    free() the buffer
    repeat until end of file is reached

    The real question is in what kind of structure do you want to put in the result or do you even want that at all.
    That gravely depends on what the data is and what do you want to do with it.

Log in to reply