# Thread: Combining four hex characters into one floating point number for output?

1. ## Combining four hex characters into one floating point number for output?

If I have an array, number[4], of these values:

number[0]=0x3F
number[1]=0xC7
number[2]=0xAE
number[3]=0x14

I want to use a printf(%f) statement to combine this array into a single number (0x3FC7AE14), and then output that as a single floating-point number (1.56).

The printf statement is the easy part. My problem is I do not know how to take the four separate array values and combine them into a single floating point number. Can someone tell me how to do this?

Thanks!

2. assuming 'number' is an array of char AND sizeof(float) == 4 on your system AND your machine uses big-endian ordering:

Code:
printf("%f\n", *((float*)number));

3. not to mention there are no guarantees that your machine will represent floats in the same format as described in your post.

4. Thanks, will give it a try!

5. memcpy( &myfloat, number, sizeof(float) );

Simply casting from one pointer type to another pointer type may get you an alignment exception.

6. I can't get either of these solutions to work. I tried this code:

Code:
char number[4];
sizeof(float)==4;

while (1)
{
number[0]=0x3F;
number[1]=0xC7;
number[2]=0xAE;
number[3]=0x14;
printf("%f\n", *((float*)number));
};
With this code I get a "declaration syntax error" on the sizeof line. I then tried this code instead:

Code:
char number[4];
float float_number;
sizeof(float_number)==4;

while (1)
{
number[0]=0x3F;
number[1]=0xC7;
number[2]=0xAE;
number[3]=0x14;
printf("%f\n", *((float_number*)number));
};
With this code I still get a "declaration syntax error" on the sizeof line, and I also get an "invalid expression" error on the printf line.

As for the second solution using memcpy, my compiler won't recognize it. It may have to do with the fact that I'm writing this code for an Atmel microcontroller.

Any further help is appreciated!

7. sizeof(float_number) is not an l-value AFAIK.

EDIT: Is it? And use CODE tags please.

8. >sizeof(float)==4;

sizeof returns the size of whatever you pass it. You have to assign the result to some variable. It looks like you're trying to check for what is the size of a float. In that case use an if() statement:
Code:
if (sizeof(float) == 4)
{
/* Do something here */
}
For now, you can probably just remove that line, and it will compile.

>As for the second solution using memcpy, my compiler won't recognize it.
Are you sure you included the proper header file (string.h)? This solution is much preferred, as it is a portable solution.

9. Originally Posted by swoopy
Are you sure you included the proper header file (string.h)? This solution is much preferred, as it is a portable solution.
Thanks swoopy, I was not including string.h. I included it, and then tried this code:

Code:
char number[4];
float float_number;

while(1)
{
number[0]=0x3F;
number[1]=0xC7;
number[2]=0xAE;
number[3]=0x14;
memcpy(float_number, number, 4);
printf("%f\n", float_number);
}
I got this error with regards to the memcpy line:

parameter #1 of type 'float' is incompatible with type 'void*' specified in the function 'memcpy' declaration

I feel like I'm almost there.

10. >parameter #1 of type 'float' is incompatible with type 'void*' specified in the function 'memcpy' declaration

Ok, if you check Salem's post, he passes an address for the first argument. This is required, because otherwise you would simply be passing a copy of the float variable, and nothing would be changed. So add & to your code:
Code:
memcpy(&float_number, number, sizeof(float_number));

11. Okay, I tried this code:

Code:
char number[4];
float output;

while(1)
{
number[0]=0x3F;
number[1]=0xC7;
number[2]=0xAE;
number[3]=0x14;
memcpy(&output,number,4);
printf("The floating point number is %f\n\r", output);
}
And it compiles without errors. But when I watch the result in a terminal window it says "The floating point number is" over and over again with a carriage return and line feed at the end. When I look at the output in hex, there are no characters whatsoever between the space after the word "is" and the \n\r. It goes right from 0x20 (space) to 0x0A & 0x0D (LF & CR) without including anything in between. Shouldn't the hex values of the floating point number be there?

12. Perhaps your endianness is reversed.
Code:
#include <stdio.h>
#include <string.h>

int main()
{
float output;
char number[4];
number[3]=0x3F;
number[2]=0xC7;
number[1]=0xAE;
number[0]=0x14;
memcpy(&output, number, sizeof number);
printf("output = %g\n", output);
return 0;
}

/* my output
output = 1.56
*/

13. Originally Posted by Dave_Sinkula
Code:
#include <stdio.h>
#include <string.h>

int main()
{
float output;
char number[4];
number[3]=0x3F;
number[2]=0xC7;
number[1]=0xAE;
number[0]=0x14;
memcpy(&output, number, sizeof number);
printf("output = %g\n", output);
return 0;
}

/* my output
output = 1.56
*/
Weird. I just copied your code into my compiler and onto the microcontroller, and all I got was "output =", no number after. I have no idea what is wrong.

14. How about trying the reverse case first?
Code:
#include <stdio.h>

int main()
{
float value = 1.56;
unsigned char *byte = (unsigned char*)&value;
size_t i;
for ( i = 0; i < sizeof value; ++i )
{
printf("byte[%lu] = 0x%02X\n", (long unsigned)i, (unsigned)byte[i]);
}
return 0;
}

/* my output
byte[0] = 0x14
byte[1] = 0xAE
byte[2] = 0xC7
byte[3] = 0x3F
*/
BTW, which micro, compiler? Is there some compiler switch that leaves out floating point formats for printf unless you explicitly request it?

[edit=2]http://www.dragonsgate.net/cgi-bin/FAQ/fom?file=43 ?

15. Originally Posted by Dave_Sinkula
BTW, which micro, compiler? Is there some compiler switch that leaves out floating point formats for printf unless you explicitly request it?
I'm using an Atmel ATmega32 with CodeVisionAVR. I checked the settings, and what do you know, yes, the default setting does NOT allow printf to output floats. So, I turned it on, and... "Evaluation version file size limit exceeded" Dang!

So now I have to buy the full version. But, nice catch man, thanks! I bet that fixes it.

And thanks to everybody else who helped. This is a great forum, and hopefully once I'm a bit more experienced I can contribute more.