LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Convert a float to unsigned char

Solved!
Go to solution

Hello,

 

I have a variable float type (example float a = 5.02) and I want to save the value into unsigned char.

I tryed to cast the value but it dosn't work 😕

unsigned char a;
float b = 5.02;

a = (char)b; 

Regards,

 

 

0 Kudos
Message 1 of 3
(6,094 Views)
Solution
Accepted by topic author BlueAnaconda

I have found a solution. 

We need to convert the float to int then to unsigned char by simple cast. 

 

Regards,

0 Kudos
Message 2 of 3
(6,077 Views)

try this:

char stringvariable [LINELEN+1];

float floatwariable;

sprintf(string,"%f",floatvariable);

or use CVI's own conversion function

"

Formatting Functions

The formatting functions format and combine multiple source items into a single target item. The only difference in the workings of the three functions is the location of the target data. For Fmt, the target is a data item in memory that you pass to the function by reference. You must pass the target parameter for Fmt by reference. For FmtFile, the target is a file whose handle you pass as the first argument. For FmtOut, the target is the Standard Output, which is typically the display. FmtOut omits the target argument present in the other two functions. Except for these differences, the following descriptions apply to all the formatting functions.

Format Strings for Formatting Functions

The formatting functions have a formatString parameter that contains the information needed to transform the source arguments to the target argument. Format strings for all the formatting functions are of the following form:

"target_spec < source_specs_and_literals"

 

0 Kudos
Message 3 of 3
(6,007 Views)