Velocity Reviews > Re: hexadecimal to float conversion

# Re: hexadecimal to float conversion

SM Ryan
Guest
Posts: n/a

 08-20-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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?

Joe Wright
Guest
Posts: n/a

 08-21-2005
SM Ryan wrote:
> (E-Mail Removed) 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 ---

pavithra.eswaran@gmail.com
Guest
Posts: n/a

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

Keith Thompson
Guest
Posts: n/a

 08-25-2005
(E-Mail Removed) 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

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.