LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

char to number

Im using LabWindows to acquire data from the serial port.
My serial port is getting  a char,The char HEX display is FB DC.( 1111 1011 1110 1100 )
I want convert a floating number.
Can you help me ?
 

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

Hello,

you could use the scanning function to achieve your goal:

Scan (ch, "%c>%f", &number);

If you're not familiar with formatting and scanning funtions, you can use the formatting wizard. You can find it at the location C:\Program Files\National Instruments\CVI80\bin\fmtwz.exe (note that the name of the CVI80 directory can differ, depending on your CVI version).

0 Kudos
Message 2 of 5
(3,874 Views)
Allan,

since CVI stands for (Ansi) C for Virtual Instrumentation, you can use all standard Ansi C-functions for typecasting. One of these functions for example is atof. Read the description:
This function converts the initial portion of a string to a double representation.
You can find more functions like this in the ANSI C-library -> General utilities -> String to Arithmetic Expressions.

Norbert B.
NI Germany
Norbert
----------------------------------------------------------------------------------------------------
CEO: What exactly is stopping us from doing this?
Expert: Geometry
Marketing Manager: Just ignore it.
0 Kudos
Message 3 of 5
(3,874 Views)

If the data you are getting from the serial port is an ascii string, the suggestions above will work for you.  But I suspect your data is binary.  It is easy to determine what kind of data you have by looking at it in the debugger.  If it is binary you can use this to convert 16 bit binary values like 0xFBDC to an unsigned short:

unsigned short intValue; 
char yourBuffer[??]       // the buffer you loaded with data from the serial port


intValue = *(unsigned short*)yourBuffer;

If you want to step through the array use:
intValue[i] = *(unsigned short*)&yourBuffer[i*sizeof(unsigned short)]; in a loop.

If the data is a signed integer, just drop the "unsigned" part.

If you are getting the data from something other than an Intel based system you may also have to adjust the format a little (the high and low bytes could appear to be swapped).  If you need help converting between other formats, post again. 

Good Luck

 

Message Edited by mvr on 08-28-2006 08:00 AM

0 Kudos
Message 4 of 5
(3,861 Views)
use ansi C data type transforming functions, they can also work in CVI
0 Kudos
Message 5 of 5
(3,835 Views)