如何字符转换成浮动 [英] How to convert Char into Float

查看:254
本文介绍了如何字符转换成浮动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何unsigned char值转换成float或double在AVR工作室4编码?

How to convert an unsigned char value into a float or double in coding in AVR studio 4.?

请帮我是初学者,我的问题可能听起来愚蠢太:/

Please help I am a beginner, my question may sound stupid too :/

就像我有pssed一个字符键$ P $

Like I have got a char keyPressed

和我一直在使用印在屏幕上
lcd_gotoxy(0,0);
lcd_puts(键pressed);

and I have printed it on the screen using lcd_gotoxy(0,0); lcd_puts (keyPressed);

现在我想用这个值来计算东西..
如何将其转换成float或double?请大家帮忙

Now I want to use this value to calculate something.. How to convert it into float or double? please help

推荐答案

如果你想例如字符'a'作为浮法65.0再做到这一点的方法是

if you want for example character 'a' as 65.0 in float then the way to do this is

unsigned char c='a';
float f=(float)(c);//by explicit casting
float fc=c;//compiler implicitly convert char into float.

如果你想例如字符'9'作为浮动9.0,然后做到这一点的方法是

if you want for example character '9' as 9.0 in float then the way to do this is

unsigned char c='9';
float f=(float)(c-'0');//by explicit casting
float fc=c-'0';//compiler implicitly convert char into float.

如果要转换包含数字这里是浮动的方式字符数组

if you want to convert character array containing number to float here is the way

#include<string>
#include<stdio.h>
#include<stdlib.h>
void fun(){
unsigned char* fc="34.45";
//c++ way
std::string fs(fc);
float f=std::stof(fs);//this is much better way to do it
//c way
float fr=atof(fc); //this is a c way to do it
}

更多参考链接:的http:// EN。 CP preference.com / W / CPP /串/ basic_string的/ STOF
http://www.cplusplus.com/reference/string/stof/

这篇关于如何字符转换成浮动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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