Re: hexadecimal to float conversion
- From: Jack Klein <jackklein@xxxxxxxxxxx>
- Date: Thu, 18 Aug 2005 21:20:58 -0500
On 18 Aug 2005 18:59:11 -0700, pavithra.eswaran@xxxxxxxxx wrote in
comp.lang.c:
> Hi,
> I would like to convert a single precision hexadecimal number to
> floating point. The following program seems to work fine..
> But I do not want to use scanf. I already have a 32 bit hexadecimal
> number and would like to convert it into float. Can anyone tell me how
> to do it?
Your post is extremely unclear, and your sample code does not seem to
match it. What is a "single precision hexadecimal number"? If you
are using scanf() you apparently have a text string. How was this
text string created?
#include <stdio.h> /* needed for prototype of scanf() */
#include <stdlib.h> /* needed for strto...() */
> int main()
> {
> float theFloat;
> {
> scanf("%f", &theFloat);
I don't understand, you seem to have a float right here already. It
appears that your implementation of scanf() understands and parses
floating point values in some hex format. Most unusual.
> printf("0x%08X, %f\n", *(int *)&theFloat, theFloat);
> }
> return 0;
> }
The truly safe alternative is to use fgets() to read the string into a
buffer and use strtod() to parse it into a double. If scanf() is
giving you the correct value, I guess strtod() will as well. But I
have a hard time believing that scanf() is turning a string of hex
into a float.
Perhaps the code you typed is not actually what your real code does?
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
.
- References:
- hexadecimal to float conversion
- From: pavithra . eswaran
- hexadecimal to float conversion
- Prev by Date: Re: getting variable length strings from stdin
- Next by Date: Re: hexadecimal to float conversion
- Previous by thread: hexadecimal to float conversion
- Next by thread: Re: hexadecimal to float conversion
- Index(es):
Relevant Pages
|