How to convert float numbers to a cstring?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to convert float numbers to a cstring?

Jump to solution
1,617 Views
AirDragon
Contributor III

CW 5.9.0

 

My project is doing quite well so far, but I need to extract the data from the MCU so that it can be manipulated on in MS Excell.

 

To do this, I've decided to convert the data into a character string, and then send it over the SCI module into HyperTerm for extraction.

 

So far I have been searching for a standard C function that will do something like this, but so far I've only come up with printf.

 

I've got a notion that I'll probably end up doing this manually, though...

Labels (1)
0 Kudos
1 Solution
945 Views
kef
Specialist I

what about sprintf ?

View solution in original post

0 Kudos
6 Replies
946 Views
kef
Specialist I

what about sprintf ?

0 Kudos
945 Views
AirDragon
Contributor III

Thanks kef,

 

That seems to work for the most part...

 

After I've spent a bit of time looking over the formatting options, I've managed to get it to output whats inside the format string... all except for the floating point number that is!

 

char cString[64]; // String bufferint n; // Size of string in the bufferfloat x; // Test numbervoid main(void){  MCUinit(); // Initialize device  SCI1CR2 |= SCI1CR2_TE_MASK // Enable Xmit  x = 1.2345;  n = sprintf( cString, "The number is %g.", x);  putString(cString); // Use recursion to output over SCI1  asm(SWI);  // Halt program and return to BDM}

 

 

 

I've tried different % flags and options, but didn't get anything. cString still has only "The number is ."

 

0 Kudos
945 Views
AirDragon
Contributor III

Hey kef, figured out that I forgot to include a library that actualy supports floating point. :smileyhappy:

0 Kudos
943 Views
amleng
Contributor IV

Hi AirDragon,


I have exactly the same problem. I can not convert float data to string by sprintf function. what library did you include to your project? I added float.h but didn't help!

0 Kudos
943 Views
bigmac
Specialist III

Hello,

 

Perhaps this thread may also be of interest if the size of the sprinf function should become problematic.  The associated binary-to-numeric ASCII conversion process was then discussed here .

 

Regards,

Mac

 

0 Kudos
945 Views
AirDragon
Contributor III

Many thanks, bigmac!

 

That'll definantly come in handy when I get around to making the project use fixed point arithmetic...

0 Kudos