LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Can I use the Scan format function to break up this string

I'm reading a string from an instrument which comes in looking like "$D025003147".  From this string I need to extract three integer values, the 2, the 5, and the 3 (the values I need will always be in the locations where the 2, 5, and 3 are in this example).  Can I do this with a single call to the Scan function (int Scan (void *Source, char Format_String[], ...); )?  I hate sounding like an idiot, but this function gets the best of me.  I think I could make good use of it if I understood it better.
 
Thank you
 
Stuart Van Deusen

 

0 Kudos
Message 1 of 5
(3,971 Views)

int  i1, i2, i3;
char a[16];

Option 1, with the use of a single Scan function as you want:
strcpy (a, "&D034007147");
n = Scan (a, "%s[i3]>%d[w1]%d[w1]%d[w2d]%d[w1]", &i1, &i2, &i3);

Option 2, directly getting character value ("0" is ascii 48, "1" is ascii 49 and so on):
strcpy (a, "&D034007147");
i1 = a[3] - 48;
i2 = a[4] - 48;
i3 = a[7] - 48;



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 2 of 5
(3,957 Views)

Hello Stuart.

If you're not familiar with the Scanning function, it might be interesting to user the Wizard. You can find this program on your hard disk: C:\Program Files\National Instruments\CVI80\bin\fmtwz.exe. Note that the name of the directory CVI80 can be different on your system, depending on your CVI version. It is possible to add a shortcut to the LabWindows/CVI Tools Menu. To do so, select "Customize" from the Tools menu and add the program.

 

0 Kudos
Message 3 of 5
(3,940 Views)

Some comment on this Scan function which is powerful and painful at the same time Smiley Wink:

n = Scan (a, "%s[i3]>%d[w1]%d[w1]%d[w2d]%d[w1]", &i1, &i2, &i3);
                  |      |     |     |      |

                  |      +-----+-----|------+---> Read an integer consuming one byte in the source
                  |                  +----------> Read 2 bytes from the source discarding them
                  +-----------------------------> Read the string starting from the 3rd character



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 4 of 5
(3,937 Views)

Thank you both very much for the replys.  Roberto, not only have you provided me with code which does exactly what I want, but your explanation will help me to use the function in the future (I learn much better by applying other examples to my situation).  Wim, I didn't know that wizard was there.  It will also be a good tool in helping me to make good use of this function.

Best regards,

Stuart Van Deusen Insert Smilies

0 Kudos
Message 5 of 5
(3,920 Views)