简体   繁体   English

将浮点数和整数转换为char *

[英]Converting floats and integers to char*

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. 我刚接触C,对使用C进行嵌入式处理非常感兴趣。我需要转换为char*并将整数和浮点数(仅0.01分辨率)输出到具有8位接口的LCD屏幕上。 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. 我已经阅读了一些帖子,并且看到了所有将intfloat转换为char*好主意,但是我不确定它们中到底发生了什么。

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 使用完全标准的C99实施,您可能会做类似的事情

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. 然后,您可以将该buf发送到LCD端口,或将其strdup以供以后使用。 (If you use strdup you'll need to free the result). (如果使用strdup ,则需要free结果)。

Read the documentation of snprintf for details. 有关详细信息,请阅读snprintf的文档。 You probably should test the return int value of snprintf . 您可能应该测试snprintf的return int值。

Since you're working on embedded programming you skould be aware of the fact that standard conversions often make use of divisions which are very taxing on any processor. 由于您正在从事嵌入式编程工作,因此您应该意识到以下事实:标准转换通常利用除法运算,这在任何处理器上都非常繁重。

Since integers are converted using divide-by-ten you could instead implement a division using multiplication by invariant integers. 由于整数是通过除以十来转换的,因此您可以使用乘以不变整数来实现除法。 This method should allow you to convert most values in the time it takes to convert a single digit in a value using division. 此方法应允许您在使用除法转换值中的一位数字时转换大多数值。

For floating point use the following steps: 对于浮点,请执行以下步骤:

  1. save the sign and take the absolute value if sign is negative 保存符号并在符号为负时取绝对值
  2. multiply by 10.0 raised to the number of decimals you need 乘以10.0升至所需的小数位数
  3. add 0.5 and convert to an integer 加0.5并转换为整数
  4. convert the integer to a string using the division method previously suggested 使用先前建议的除法将整数转换为字符串
  5. make sure the string is at least number of decimals + 1, insert ascii 0 at beginning as necessary 确保字符串至少为小数位数+ 1,并根据需要在开头添加ascii 0
  6. insert a minus sign at the beginning as required 根据需要在开头插入减号
  7. insert a decimal point at the appropriate position 在适当位置插入小数点

Here is an example that requires two decimals 这是一个需要两个小数的示例

3758.3125
375831.25    (multiply by 10^2)
375831.75    (add 0.5)
375831       (convert to integer)
"375831"     (convert to string)
"3758.31"    (insert decimal point => final result)

A somewhat more difficult case 更加困难的情况

-0.0625
0.0625       (keep sign)
6.25         (multiply by 10^2)
6.75         (add 0.5)
6            (convert to integer)
"6"          (convert to string)
"006         (insert ascii 0 as required)
"-006"       (insert minus sign)
"-0.06"      (insert decimal point => final result)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM