# Re: hexadecimal to float conversion

Discussion in 'C Programming' started by SM Ryan, Aug 20, 2005.

1. ### SM RyanGuest

wrote:
# I am doing embedded programming and coding using NIOS processor.
# I am reading a 32 bit value from a register and storing it in a 32 bit
# data type.
# for e.g.
#
# alt_u32 gain = 0x42ED4000;

If that is a floating point value encoded as hex string,
you might be able to do
union {
int i;
float f;
} switcheroo;
switcheroo.i = 0x42ED4000;
floatvalue = switcheroo.f;
or perhaps
int i = 0x42ED4000;
float f;
memcpy(&i,&f,sizeof f);

Depends on what exactly the hex string is encoding. There could also
be byte flopping issues.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
Haven't you ever heard the customer is always right?

SM Ryan, Aug 20, 2005

2. ### Joe WrightGuest

SM Ryan wrote:
> wrote:
> # I am doing embedded programming and coding using NIOS processor.
> # I am reading a 32 bit value from a register and storing it in a 32 bit
> # data type.
> # for e.g.
> #
> # alt_u32 gain = 0x42ED4000;
>
> If that is a floating point value encoded as hex string,
> you might be able to do
> union {
> int i;
> float f;
> } switcheroo;
> switcheroo.i = 0x42ED4000;
> floatvalue = switcheroo.f;
> or perhaps
> int i = 0x42ED4000;
> float f;
> memcpy(&i,&f,sizeof f);
>
> Depends on what exactly the hex string is encoding. There could also
> be byte flopping issues.
>

This one..

#include <stdio.h>
int main(void) {
union {
int i;
float f;
} switcheroo;
switcheroo.i = 0x42ED4000;
printf("0x%X %f\n", switcheroo.i, switcheroo.f);
return 0;
}

...prints..
0x42ED4000 118.625000
--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---

Joe Wright, Aug 21, 2005

3. ### Guest

Hi,
Thanks a lot for your solutions. I was making some error in
typecasting. It works fine now.

, Aug 25, 2005
4. ### Keith ThompsonGuest

writes:
> Thanks a lot for your solutions. I was making some error in
> typecasting. It works fine now.

Solutions to what?

Search this newsgroup for the phrase "Context, dammit!", and follow