将浮点数和整数转换为char * [英] Converting floats and integers to char*

查看:422
本文介绍了将浮点数和整数转换为char *的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C的新手,并且被C推向嵌入式处理领域。我需要转换为 char * 并输出整数和浮点数(只有0.01分辨率)到具有8位接口的LCD屏幕。我一直在阅读一些帖子,我看到所有这些伟大的想法,将 int float code> char * 但我不知道他们发生了什么。



有人可以为我的两个查询提供一个方法和一点解释吗?

解决方案

它实际上取决于标准库,在某些嵌入式系统中,库是部分实现的,甚至不存在。通过完全标准C99 实施,您可以执行类似于

  char buf [40]; 
int i;
double x;
//计算i& x,然后用::
snprintf(buf,sizeof(buf)),i =%2d x =%。2f,i,x)从中创建一个字符串;

然后,您可以将 buf 发送到您的LCD端口,或 strdup 以供以后使用。 (如果您使用 strdup ,则需要免费结果)。



有关详细信息,请阅读 snprintf 的文档。您可能应该测试 int 的返回值 snprintf


I'm new to C and was kind of thrust into this world of embedded processing with C. I need to convert to char* and output integers and floating point numbers (only 0.01 resolution) to an LCD screen with an 8 bit interface. I've been reading through some of the posts and I see all these great ideas for converting int and float to char* but I'm not exactly sure what's going on in them.

Can someone provide a method for my two queries and a little explanation?

解决方案

It actually depends upon the standard library, and in some embedded systems that library is partially implemented, or even absent. With a fully standard C99 implementation you might do something like

char buf[40];
int i;
double x;
// compute i & x, then make a string from them with::
snprintf(buf, sizeof(buf), "i=%2d x=%.2f", i, x);

Then you might send that buf to your LCD port, or strdup it for later usage. (If you use strdup you'll need to free the result).

Read the documentation of snprintf for details. You probably should test the return int value of snprintf.

这篇关于将浮点数和整数转换为char *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆