Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Re: hexadecimal to float conversion (http://www.velocityreviews.com/forums/t439139-re-hexadecimal-to-float-conversion.html)

SM Ryan 08-20-2005 08:33 PM

Re: hexadecimal to float conversion
 
pavithra.eswaran@gmail.com 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 08-21-2005 01:12 AM

Re: hexadecimal to float conversion
 
SM Ryan wrote:
> pavithra.eswaran@gmail.com 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 08-25-2005 06:51 PM

Re: hexadecimal to float conversion
 
Hi,
Thanks a lot for your solutions. I was making some error in
typecasting. It works fine now.


Keith Thompson 08-25-2005 07:49 PM

Re: hexadecimal to float conversion
 
pavithra.eswaran@gmail.com 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
the advice you find.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <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.


All times are GMT. The time now is 07:35 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.