Re: sscanf trouble

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, 2011-01-25 at 11:09 +0000, Jonathan Andrews wrote:
> Hi people.
> 
> Can anyone help with what should be a simple problem. I have some text
> in the following form. Its a temperature reading from a wireless sensor,
> the format is T<sensor number>=<float ish> <TAB><TAB><Two digit ascii
> checksum>
> 
> T1=18.0<TAB><TAB>XX\n\r
> 
> Some examples:
> 
>         T1=-11.5        EA
>         T1=24.0         9D
> 
> Im trying to convert this string into a sensor ID, a floating point
> reading and a checksum as 3 variables using sscanf
> 
> sscanf(line,"T%d=%f\t\t%X",&sensorid,&temperature,&checksum);
> 
> No amount of variations on a theme seem to give me an entire decode
> here, seems the equals seems to screw things up.  Anyone any ideas how I
> can make this work?
> 
> Thanks for any advice,
> Jon

Compiled the following using gcc v4.4.5 on a Fedora 13 system and it
seemed to work fine:

#include <stdio.h>

main()
{
    int sensorid, checksum;
    float temperature;
    char *line1 = "T1=-11.5\t\tEA\n\r";
    char *line2 = "T1=24.0\t\t9D\n\r";

    sscanf(line1, "T%d=%f\t\t%X", &sensorid, &temperature, &checksum);
    printf("[1] SensorID = [%d], Temp = [%f], Checksum = [0x%X]\n",
sensorid, temperature, checksum);

    sscanf(line2, "T%d=%f\t\t%X", &sensorid, &temperature, &checksum);
    printf("[2] SensorID = [%d], Temp = [%f], Checksum = [0x%X]\n",
sensorid, temperature, checksum);
}

The output was:

[1] SensorID = [1], Temp = [-11.500000], Checksum = [0xEA]
[2] SensorID = [1], Temp = [24.000000], Checksum = [0x9D]

Your sscanf() call worked as expected.  Maybe your input isn't exactly
what you think (i.e. spaces instead of a tab)?

-- 
Adam Stein @ Xerox Corporation       Email: adam@xxxxxxxxxxxxxxxxxx

Disclaimer: Any/All views expressed
here have been proven to be my own.  [http://www.csh.rit.edu/~adam/]



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux