如何将文本转换为C中的浮点数
我正在编写一个代码,该代码将解析传入的UART消息并提取我想要的信息。我将发送一个命令,然后发送一个浮动号码:
“ set_voltage:2.4”
我想知道如何将“ 2.4”放在实际浮点数为2.4 我试图使用atof函数,但它可以使用。可能是因为我有“。”中间。
我有一个保存“ 2.4”的temp_buffer,我需要将其转换为浮点数。
我的temp_buffer包含以下字节:
temp_buf [0] = 50
temp_buf [1] = 46
temp_buf [2] = 53
temp_buf [3] = 0
我的测试代码:
float number;
float number2;
char test_buf[4] = {50,46,53,0};
for(int i = 0; i < 4 ; i++){
printf("test_buf[%i] = %u \n",i,test_buf[i]);
}
number = atof(test_buf);
number2 = strtod(test_buf,NULL);
printf("FLOAT NUMBER = %.2f \n",number);
printf("FLOAT NUMBER2 = %.2f \n",number2);
printf输出:
test_buf[0] = 50
test_buf[1] = 46
test_buf[2] = 53
test_buf[3] = 0
FLOAT NUMBER = 0.00
FLOAT NUMBER2 = 0.00
更新尝试strtod : 我试图使用为我建议的功能:
static double get_double(const char *str)
{
/* First skip non-digit characters */
/* Special case to handle negative numbers */
while (*str && !(isdigit(*str) || ((*str == '-' || *str == '+') && isdigit(*(str + 1)))))
str++;
/* The parse to a double */
return strtod(str, NULL);
}
在我的主体中,我有:
double number;
double number2;
char test_buf[4] = {50,46,53,0};
char test_buf2[4] = {'2','.','5','\0'};
number = get_double(test_buf);
number2 = get_double(test_buf2);
printf("number = %d \n",number);
printf("number2 = %d \n",number2);
似乎仍然不起作用,串行监视器输出:
number = 0
number2 = 0
update2尝试strtof
char test_buf2[4] = {'2','.','5','\0'};
char test_buf3[] = "2.5";
float float1 = strtof(test_buf2, NULL);
printf("float1 = %.2f \n",float1);
float float2 = strtof(test_buf3, NULL);
printf("float2 = %.2f \n",float2);
结果:
float1 = 1075838976.00
float2 = 1075838976.00
update3包括#include&lt; stdlib.h&gt; ,
事实证明,要使strtof work wurk,我也必须包括“ stdlib.h”,即使我戴了任何警告。现在,strtof似乎效果很好,但Strtod仍然不起作用。
I am writing a code that would parse incoming UART messages and extract the information that I want. I will be sending a command and then a floating number for example:
"SET_VOLTAGE:2.4"
I would like to know how can I parse the "2.4" in to an actual float number 2.4
I have tried to use atof function but it didint work. Probably because I have "."in the middle.
I have a temp_buffer that holds "2.4"and I need to convert it to float number.
My temp_buffer contains the following bytes:
temp_buf[0]=50
temp_buf[1]=46
temp_buf[2]=53
temp_buf[3]=0
My test code:
float number;
float number2;
char test_buf[4] = {50,46,53,0};
for(int i = 0; i < 4 ; i++){
printf("test_buf[%i] = %u \n",i,test_buf[i]);
}
number = atof(test_buf);
number2 = strtod(test_buf,NULL);
printf("FLOAT NUMBER = %.2f \n",number);
printf("FLOAT NUMBER2 = %.2f \n",number2);
printf output:
test_buf[0] = 50
test_buf[1] = 46
test_buf[2] = 53
test_buf[3] = 0
FLOAT NUMBER = 0.00
FLOAT NUMBER2 = 0.00
UPDATE trying strtod:
I have tried to use a function that has been suggested for me:
static double get_double(const char *str)
{
/* First skip non-digit characters */
/* Special case to handle negative numbers */
while (*str && !(isdigit(*str) || ((*str == '-' || *str == '+') && isdigit(*(str + 1)))))
str++;
/* The parse to a double */
return strtod(str, NULL);
}
In my main.c I have:
double number;
double number2;
char test_buf[4] = {50,46,53,0};
char test_buf2[4] = {'2','.','5','\0'};
number = get_double(test_buf);
number2 = get_double(test_buf2);
printf("number = %d \n",number);
printf("number2 = %d \n",number2);
That still does not seem to work, the serial monitor output:
number = 0
number2 = 0
UPDATE2 Trying strtof
char test_buf2[4] = {'2','.','5','\0'};
char test_buf3[] = "2.5";
float float1 = strtof(test_buf2, NULL);
printf("float1 = %.2f \n",float1);
float float2 = strtof(test_buf3, NULL);
printf("float2 = %.2f \n",float2);
The results:
float1 = 1075838976.00
float2 = 1075838976.00
UPDATE3 Include #include <stdlib.h>
It turns out that in order for strtof to wurk I must include "stdlib.h"even though I didint get any warnings. Now strtof seems to work fine but strtod still does not work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论