How to convert float numbers to a cstring?

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

How to convert float numbers to a cstring?

跳至解决方案
2,964 次查看
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...

标签 (1)
0 项奖励
回复
1 解答
2,292 次查看
kef
Specialist I

what about sprintf ?

在原帖中查看解决方案

0 项奖励
回复
6 回复数
2,293 次查看
kef
Specialist I

what about sprintf ?

0 项奖励
回复
2,292 次查看
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 项奖励
回复
2,292 次查看
AirDragon
Contributor III

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

0 项奖励
回复
2,290 次查看
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 项奖励
回复
2,290 次查看
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 项奖励
回复
2,292 次查看
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 项奖励
回复