字符数组到浮点数的转换 [英] character array to floating point conversion
本文介绍了字符数组到浮点数的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试转换输出缓冲区(字符数组)将下面的代码转换为浮点格式以供进一步计算.谁能告诉我怎么做.
I am trying to convert the output buffer(character array) of the code below to floating point format for further calculations. Can anybody tell me how to do it.
#include "usbtmc.h"
#include <errno.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <getopt.h>
#include <inttypes.h>
#include <sys/types.h>
#include <pthread.h>
int main()
{
int myfile;
char buffer[4000];
int actual;
myfile=open("/dev/usbtmc1",O_RDWR);
if(myfile>0)
{
system("echo MEAS:VOLT:AC?>/dev/usbtmc1");
actual=read(myfile,buffer,4000);
buffer[actual] = 0;
printf("Response =
%s
",buffer);
close(myfile);
}
return 0;
}
此代码的示例输出是
响应 =+1.29273072E-04
推荐答案
你可能有两种方法:
using double
atof
(const char* str)
float f;
f = (float)atof(buffer);
printf("%f",f); // here you can use f
using int sscanf
( const char * s, const char * format, ...)
float f;
sscanf(buffer,"%f",&f);
printf("%f",f); // here you can use f
这篇关于字符数组到浮点数的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文