473,379 Members | 1,544 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

hexadecimal to float conversion

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?

int main()
{
float theFloat;
{
scanf("%f", &theFloat);
printf("0x%08X, %f\n", *(int *)&theFloat, theFloat);
}
return 0;
}

Nov 15 '05 #1
10 22571
On 18 Aug 2005 18:59:11 -0700, pa**************@gmail.com 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/~a...FAQ-acllc.html
Nov 15 '05 #2
pa**************@gmail.com writes:
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?

int main()
{
float theFloat;
{
scanf("%f", &theFloat);
printf("0x%08X, %f\n", *(int *)&theFloat, theFloat);
}
return 0;
}


That code (if you add the necessary #include directive) reads a
floating-point value from stdin and prints a hexadecimal string.
That's the opposite of what you're asking for, so saying it "seems to
work fine" is odd.

Your code assumes that int and float have the same size (and probably
the same alignment requirements). This is not a safe assumption.

You need to define exactly what you mean by "a single precision
hexadecimal number". I *think* you mean a hexadecimal string
corresponding to the representation of the floating-point value. If
so, this is also going to be non-portable.

For example, this program:

#include <stdio.h>

int main(void)
{
float f = 42.5;
unsigned int u = *(unsigned int*)&f;
printf("f = %g\n", f);
printf("u = 0x%X\n", u);
return 0;
}

happens to print

f = 42.5
u = 0x422A0000

on one implementation where I just tried it, but the hexadecimal
digits could be different on different systems. Most systems these
days use IEEE floating-point formats, but byte order can vary.

--
Keith Thompson (The_Other_Keith) ks***@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.
Nov 15 '05 #3
pa**************@gmail.com wrote:
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?

int main()
{
float theFloat;
{
scanf("%f", &theFloat);
This reads a group of characters in the usual form for
a floating-point constant, converts them to a `float' value,
and stores the result in `theFloat'. Or perhaps it fails
because there's no more input, or because the input stream
provided "blue Hawaii" instead of something that looks like
a number, or for some other reason. You should check the
value returned by the scanf() function to see whether it has
actually converted and stored a number.
printf("0x%08X, %f\n", *(int *)&theFloat, theFloat);


The output may or may not be meaningful; on some machines
it might not work at all (for example, you might get SIGBUS
or some other kind of trap). You are trying to pretend that
the representation of the floating-point value `theFloat' can
also be understood as an `int', and this may or may not be so.
Many machines will give some kind of result, but different
machines are likely to give different results, and the C language
itself doesn't guarantee any result at all.

C does provide one special guarantee for a restricted form
of type-punning: it is permitted to access the individual bytes
of any object's representation through a pointer of the type
`unsigned char*'. For example, you could do

unsigned char *p = (unsigned char*)&theFloat;
printf ("0x");
while (p < (unsigned char*)(&theFloat + 1))
printf ("%02X", *p++);
printf (", %f\n", theFloat);

to print the individual bytes one at a time. (Even this isn't
perfect, by the way: a byte can have more than eight bits and
hence more than two hexadecimal digits. Machines where this is
the case are rare, but do exist.) This loop is guaranteed to
produce output and not to trap or anything like that, but the
output you get from machine A may not match that from machine B.

However, you actually asked about going the other way: from
a stream of hexadecimal digits to a floating-point value. You
could use a loop to convert the bytes one at a time (the inverse
of what's shown above), but the byte stream that produces 42.0
on machine A might produce -273.16 on machine B, or a NaN, or
even a signalling NaN that traps when you try to use it. There
is no One True Representation of a floating-point value as a
hexadecimal string -- so what, exactly, are you trying to do?

--
Eric Sosman
es*****@acm-dot-org.invalid

Nov 15 '05 #4
Mac
On Thu, 18 Aug 2005 18:59:11 -0700, pavithra.eswaran wrote:
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?

int main()
{
float theFloat;
{
scanf("%f", &theFloat);
printf("0x%08X, %f\n", *(int *)&theFloat, theFloat);
}
return 0;
}


Your post is very unclear. If you are trying to avoid scanf() have a look
at sscanf(), which behaves largely the same way but scans from a string
instead of from standard input.

If you are trying to move in the opposite direction from the code snippet
you posted above, please post the code which shows an initialization of
your "32-bit hexadecimal number" which you would like to convert to float.

--Mac

Nov 15 '05 #5
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;

[alt_u32 is a data type provided by NIOS]
This is the value, which I need to convert it into float. Simply
typecasting it into float doesn't seem to yield the correct value of
float. The above code which uses scanf does. Hence posted the above
question.

Nov 15 '05 #6
pa**************@gmail.com writes:
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;
But that's not reading a value from a register; you're specifying the
literal value in your program.
[alt_u32 is a data type provided by NIOS]
Ok, so alt_u32 is a 32-bit unsigned integer type, and you have some
mechanism to read a 32-bit value into an object of that type.

What you're describing has nothing to do with hexadecimal.
Hexadecimal is just one of a number of ways the value can be
displayed, but it's just a 32-bit integer.
This is the value, which I need to convert it into float. Simply
typecasting it into float doesn't seem to yield the correct value of
float. The above code which uses scanf does. Hence posted the above
question.


Casting an integer value to type float does a numeric conversion. You
want (I think) to re-interpret the alt_u32 value as a value of type
float; same bits, different meaning.

I think you want to do something like this:

alt_u32 gain = get_value_from_register();
float real_gain = *(float*)&gain;

This takes the address of gain, converts it from pointer-to-alt_u32 to
pointer-to-float, and dereferences the converted pointer.

This is higtly non-portable, but it sounds like your code needs to be
system-specific anyway, so that's probably not a fatal problem.

--
Keith Thompson (The_Other_Keith) ks***@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.
Nov 15 '05 #7
pa**************@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?
Nov 15 '05 #8
SM Ryan wrote:
pa**************@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 ---
Nov 15 '05 #9
Hi,
Thanks a lot for your solutions. I was making some error in
typecasting. It works fine now.

Nov 15 '05 #10
pa**************@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) ks***@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.
Nov 15 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Cesar Andres Roldan Garcia | last post by:
Hi I'm trying to write an hexadecimal file... I mean not a text plain... I have to convert a float decimal number in float hexadecimal one, and that's done. That number is the one I'm gonna...
4
by: James DeClerk | last post by:
Hi everyone. I'm new to C++ and I've got a seemingly tough problem to tackle. I have a union. This union needs to be converted into hexadecimal format. The hexadecimal number is then...
5
by: Damon | last post by:
I'm getting '', hexadecimal value 0x02, is an invalid character when I'm deseralizing XML from a 3rd party XML gateway. How do I get rid of these hexadecimal values before I deserialize? Cheers...
2
by: akash deep batra | last post by:
hi i want to convert a 96 bit binary number into a hexadecimal number. e.g binary number= 001100010001010000100101011110111111010101110100010110000101011000101010000000000000000000000000 how...
8
by: Vijay | last post by:
Hi , I am doing a small project in c. I have a Hexadecimal file and want to convert into ascii value. (i.e., Hexadecimal to Ascii conversion from a file). Could anyone help me? Thanks in...
7
by: elliotng.ee | last post by:
I have a text file that contains a header 32-bit binary. For example, the text file could be: %%This is the input text %%test.txt Date: Tue Dec 26 14:03:35 2006...
6
by: Andrea | last post by:
Hi, suppose that I have a string that is an hexadecimal number, in order to print this string I have to do: void print_hex(unsigned char *bs, unsigned int n){ int i; for (i=0;i<n;i++){...
6
by: sweeet_addiction16 | last post by:
hello Im writin a code in c... can sum1 pls help me out in writing a c code to convert decimalnumber to hexadecimal number.The hexadecimal number generated has to be an unsigned long.
8
by: d major | last post by:
I was very puzzled about the conversion between float and long, I cann't understand why a long val can convert to a float, as the below codes show: typedef unsigned long u_long; float val =...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.