LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Convert ViChar to double.

I am trying to convert a ViChar to a double and have tried using the following exmaple...
 
static ViChar daqmeasure[256];
double measurement;
 
measurement = (double) daqmeasure;
 
 
I am getting the following error...
Cast from pointer to ViChar to double is illegal.
Can someone explain a possible solution?\
 
ThanksNAdvance
0 Kudos
Message 1 of 6
(4,223 Views)

Assuming the ViChar daqmeasure[256] variable contains a floating point double value in binary form (and is not an ascii string) you can convert it by

measurement = *(double*)daqmeasure;
daqmeasure is a pointer, so CVI would not let you cast it to a double directly.

If you want to step through the array use
measurement[i] = *(double*)&daqmeasure[i*sizeof(double)]; in a loop.

If the data in ViChar is really a string value (easy to see by looking at the array in the debugger) you can use atof() to convert the string to a double value or if it is an array of doubles stored in a string use the CVI function Scan().
 

 

0 Kudos
Message 2 of 6
(4,209 Views)

It is a string but your instructions were not clear...

 

 

Thanks!

0 Kudos
Message 3 of 6
(4,208 Views)

Maybe:

    measurement = atof (daqmeasure);

will meet your needs?

JR

0 Kudos
Message 4 of 6
(4,194 Views)
JR_2005
 
I'm new to LabWindows and had never seen or heard of the atof command.
 
I apologize for my stupor.
 
Thanks alot!
FredTest
0 Kudos
Message 5 of 6
(4,180 Views)
Hi Fred,

The ViChar data type is just like a regular char datatype; however, VISA has placed a wrapper around it to increase portability. So, simple ViChar casting would work in the following situation:

ViChar mychar;
double mydouble;
mydouble = (double) mychar;

However, your case is different beacuse ViChar is an array. In your case you can use one of two methods to convert the data to a double.

1) If the data is a string in the character array, for instance:

daqmeasure[0] = '7';
daqmeasure[1] = '.';
daqmeasure[2] = '3';
daqmeasure[3] = '2';

Then you should use the atof function:

measurement = atof (daqmeasure);

2) If the data is in a binary string you can follow Function to Convert Binary in String Format to a Float or Double.
With some minor tweaks, this could help you convert a binary string to a double.

Matt Mueller
NI
0 Kudos
Message 6 of 6
(4,177 Views)