# char2float()

Hello everyone!

Before buying an arduino for myself, i was exploring some code snippets.

Came across this function char2float(unsigned int,unsigned int)
I figured it returns a 32 number taking two 16bit numbers. But i do not have clarity as to if it returns an IEEE-754 floating point number or just concatenates the two 16bit numbers.

You have only provided the signature of the function, not the function itself, so we cannot possibly tell you what it does. You need to post the code of the function.

The name indicates that it converts a char array to a floating point value, like 'atof()', yet it takes two ints as parameters. (Poorly named.)

Can you please give us a link to where you saw it?

Forgive as i thought it was an inbuilt standard function of sorts.
Turns out it was explicitly defined in one of the MODBUS Libraries. This code was used to concatenate two chars and convert the number to a floating number, as MODBUS protocol defines register size as 16bits and two registers hold the floating number.

here is the original code:

``````float char2float(unsigned char ch1,unsigned char ch2) //Function to convert 2 unsigned chars into 1 floating point value
{
unsigned long l1=0;
l1=long(ch1);
l1=l1<<16|ch2;

float num=float(l1);

return num;
}
``````

This code might not work properly and I have modified it somewhat to make it more robust, please add in your suggestions as well.

``````float uint2float(unsigned int ch1,unsigned int ch2) //Function to convert 2 unsigned ints(16bits) into 1 floating point value(32bits)
{
unsigned long l1=0;
l1=long(ch1);
l1=l1<<16|ch2;

float num=float(l1);

return num;
}
``````

Changing the name of the function is hardly going to make the function more robust. Shifting an 8 bit value 16 bits to the left, and adding another 8 bit value is NOT going to do a damned thing useful. That function, if it is exactly as you copied it, was useless crap.

PaulS:
Changing the name of the function is hardly going to make the function more robust. Shifting an 8 bit value 16 bits to the left, and adding another 8 bit value is NOT going to do a damned thing useful. That function, if it is exactly as you copied it, was useless crap.

It definitely doesn't create a floating point value.

Notes:

1. The purpose of the function should have been documented with the function code. The quality of the library must be very poor.
2. The purpose is probably to imbed characters in a float so they can be transmitted or stored as a float (reason unknown, see #1).