6

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 :/

Like I have got a char keyPressed

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

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

Mohit Goyal
  • 110
  • 1
  • 3
  • 11

3 Answers3

14

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.

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
}

for more refer to link: http://en.cppreference.com/w/cpp/string/basic_string/stof http://www.cplusplus.com/reference/string/stof/

Himanshu Pandey
  • 726
  • 5
  • 13
  • Thanks for the response.. But my char value is not constant, it is fed every time using a keypad.. say it is 16.853 Now I want to convert this into a float or double to be used further.. :/ – Mohit Goyal Aug 28 '13 at 17:27
  • That is not a `char` but a `set of characters` – Uchia Itachi Aug 28 '13 at 17:28
  • 1
    You are confusing "a char" with a character *string*. Look up `atof` (but first look up "how to use strings in C). – Jongware Aug 28 '13 at 17:30
  • Yeah I am sorry, it is a character string – Mohit Goyal Aug 28 '13 at 17:31
  • Well My actual problem is here.. http://stackoverflow.com/questions/18490942/calculator-based-on-atmega8-using-avrstudi No one responded to it so I was trying to do step by step – Mohit Goyal Aug 28 '13 at 17:47
3

For character array input you can use atof.

0xF1
  • 6,046
  • 2
  • 27
  • 50
0

Try this it works for me without any built in function or header file use.

    #include <iostream>
    using namespace std;
      int main(){
            int b[6];
            int i = 0, counter = 0;
            bool found = false;
            float temp,temp2;
            char a[6];
            cin >> a;
            while (a[i] != '\0')
            {
                b[i] = a[i];
                if (b[i] == 46){
                    found = true;
                }else{
                    b[i] -= 48;
                }if (found != true){
                counter++;
                }
             i++;
            }
           for (int j = 0; j < counter; j++) {
            temp *= 10;
            temp += b[j];
            }
            int dif = i - counter - 1;
            
         for (int j = counter+1; j <=counter+dif; j++) 
            {
            temp2 *= 10;
            temp2 += b[j];
            }
            for (int k = dif;dif>0;dif--){
            temp2 *=0.1;
            
           }
             temp+=temp2;
            cout << "\nfloat = " << temp << endl;
        }