# Char array to float

I have five individual numbers stored in a char array.

As an example the decimal numbers 12345 are stored in a char array[5].

array[0] being 1 and array[4] being 5.

I want to convert the array into a float like 123.45 and store into a EEPROM ( I know that once the conversion to float is done, storing into the EEPROM is easy )

Any tips ? Thanks in advance .

The atof() function comes to mind. Keep in mind that 12345 is NOT a floating point value. You might want to convert to an int, using atoi(), first, and then divide by 100.0 to get a float.

You need the character array to be dimensioned to at least 6, and include a terminating zero byte for atof() to work.

Why are you storing the numbers in a char array in the first place?

:)

PaulS and jremington have given the tips for way forward. Will do.

Still I wanted to clarify the point which you raised as to why would I store in char array a float value !

Its like this :

As part of a user data entry I need to get a value from user which is of the form 123.45.

For getting the value I ask the user to push some buttons on a keypad to choose the digit he wants to alter and then alter it. ( one key is used for shift and pressing it moves the cursor underneath the digits on LCD screen. Once at the required position, he pushes another key which increments the value by one and rolls over to zero when nine is reached. ) So based on his key pushes I know what value he wants to input but only digit by digit. Thus I end up with 5 nos char values and now comes the part of combine / convert them to a float of value = 123.45.

In fact this is a very common way of getting user to input numeric values. And if there is any elegant method to do this I would like to know.

Mogaraghu: In fact this is a very common way of getting user to input numeric values. And if there is any elegant method to do this I would like to know.

Arithmetic.

First, you have to know if you are using the characters '0', '1', etc., or the numeric values 0, 1, etc. In code, `'0'` (with quotes) means the character '0', whereas `0` (without quotes) means the numeric value zero. If you are using characters, first convert to numeric values. (You can convert characters '0' through '9' to their numeric values by subtracting '0'. So `'0' - '0'` gives you `0`; `'1' - '0'` gives you `1`; etc.)

Once you have the numeric values, use arithmetic to combine them. For example,

(3 * 10000) + (2 * 1000) + (7 * 100) + (6 * 10) + 8 = 32768

(9 * 10000) + (9 * 1000) + (9 * 100) + (9 * 10) + 9 = 99999

In code, I suggest you use `10000L`, `1000L`, etc., because the `L` forces the use of `long` integers, which you will need if you are going all the way to 99999.

How to insert the decimal point? Again: arithmetic! Just divide by `100.0` (you will want the decimal point there as well).

Yes master ! Its very clear.

‘0’ -‘0’ gives you 0 and ‘1’ -‘0’ gives you 1. I suppose this what is called NexGen maths

Ok while we in the basic arithmetic mode what exactly is the difference between a char variable and byte variable. As I see the byte always has a max of 255 but the char, if signed, can have only 127.

But I am sure the byte was brought in for a different purpose. What would be that ??