Re: hexadecimal to float conversion



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
.



Relevant Pages

  • Re: weird problem
    ... I already told you that the comparison between an integer and a float ... to strcmpwhich expects a pointer to a string. ... And now a question about something else: why do you use floating ... int,float, char, etc. ...
    (comp.lang.c)
  • Re: weird problem
    ... I already told you that the comparison between an integer and a float ... And now a question about something else: why do you use floating ... use then to copy a float into a char *1. ... binary representation doesn't resemble a string like "123.46343" ...
    (comp.lang.c)
  • Re: Question for a REAL expert on casting double to float...
    ... Scanf actually treats "%d" as a signed integer. ... sscanf uses %f for a float rather than a double. ... version of the format specifiers) for the correct action, ... floating point input format. ...
    (comp.lang.c)
  • Re: numpy: handling float(NaN) different in XP vs. Linux
    ... Microsoft Windows XP ... ValueError: invalid literal for float(): NaN ... Convert a string or a number to floating point. ...
    (comp.lang.python)
  • Re: Converting floats to Strings and back
    ... > I'm trying to convert a String to a float, do some arithmetic on it, then ... > The code works fine until I exceed 8 digits. ... the nasty things that happen with floating point, ...
    (comp.lang.java.programmer)