# LabWindows/CVI

cancel
Showing results for
Did you mean:

Solved!
Go to solution

## convert string to a number

I have an instrument with a serial output. The output string is   X=+03.22 CR LF Y=-04.22 CR LF.  I need to extract the two numbers.

I have been trying to use strtod in the stdlib. Is there an example code showing how to make and use character strings?

Message 1 of 7
(5,255 Views)

## Re: convert string to a number

Hi,

you might consider scanf instead. Refer to userint\standardio.cws for an example of using the scanf function.

Message 2 of 7
(5,254 Views)
Solution
Accepted by Jim Rogers

## Re: convert string to a number

If this is the only thing your instrument sends out, X and Y values one after the other, then you can do this:

char buffer[16];

double valX, valY;

ComRdTerm (port, buffer, 15, 13); // 13 = CR byte value

valX = atof (&buffer[2]);

ComRdTerm (port, buffer, 15, 13);

valY = atof (&buffer[2]);

S. Eren BALCI
IMESTEK
Message 3 of 7
(5,245 Views)

## Re: convert string to a number

Thanks for the help. Atof fixed the prob

Jim R

Message 4 of 7
(5,233 Views)

## Re: convert string to a number

I need to correct my code a little. In order to atof work correctly it must see a null character to understand where the string ends.

Here is the corrected version.

char buffer[16];

int len;

double valX, valY;

len = ComRdTerm (port, buffer, 15, 13); // 13 = CR byte value

buffer[len] = 0;

valX = atof (&buffer[2]);

ComRdTerm (port, buffer, 15, 13);

buffer[len] = 0;

valY = atof (&buffer[2]);

S. Eren BALCI
IMESTEK
Message 5 of 7
(5,213 Views)

## Re: convert string to a number

Sorry... but I would suggest one more (minor) change

char buffer[16];

int len;

double valX, valY;

len = ComRdTerm (port, buffer, 15, 13); // 13 = CR byte value

buffer[len] = 0;

valX = atof (&buffer[2]);

len = ComRdTerm (port, buffer, 15, 13);

buffer[len] = 0;

valY = atof (&buffer[2]);

Message 6 of 7
(5,209 Views)

## Re: convert string to a number

Thank you Wolfgang, I had forgotten to put it there...

Now we have a complete code!... 😉

S. Eren BALCI
IMESTEK
Message 7 of 7
(5,204 Views)