We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

# Real number to Hex conversion

on
Medium Priority
764 Views
Hi there,

I have a programming assignment in which I have to convert
a C-double precision real number into a Hexadecimal number (and vice versa) MANUALLY -- that is, extracting the sign bit, exponent bit, mantissa bits and exponent bits using masks, performing bitwise operations and calculations and making the conversion from there.  For example, if a user enters in 0.123456, the program would output the Hexadecimal form/equivalent of 0.123456.  How do I go about doing this?
Comment
Watch Question

## View Solution Only

Commented:
For the extraction look at either modf() or frexp().

double modf(double value, double *iptr);
The modf() function breaks the argument value into integral and fractional parts, each of which has the same sign as the argument.  It stores the integral part as a double in the object pointed to by iptr.

double frexp(double value, int *exp);
The frexp() function breaks a floating-point number into a normalized fraction and an integral power of 2.  It stores the integer in the int object pointed to by exp.

Your question does not make it clear as to the output format. You ask for a hexadecimal representation of something. But what?

Commented:
To answer Warren Dales's question, the output format would be
the hexadecimal equivalent of the floating-point real number.  For example, if a user entered in 0.123456 (read in as a double-precision number), the program would output the HEX equivalent of 0.123456 (not sure if it'd be represented as a signed or unsigned int).  Also, how would we go about reversing the process (that is, converting a hex number to a floating-point real number?)  If it helps, I'll be running this program using GNU's gcc compiler for UNIX running on a VAX, where gcc implements an integer as 32 bits wide.

Commented:
Edited text of question
Commented:
/* From the code below you should be able to figure out how to
do it, I tested it using Watcom, but should compile with GNU C too. My E-Mail is virgo21@hotmail.com. */

#include <stdio.h>
#include <conio.h>

main()
{
int i,j;
double x;
char *byte, bits[64];

byte = (char *) &x + sizeof (x) - 1;
// We let byte point to the last byte in the double;

for (i = 1; i <= sizeof (x); i++, byte--, printf(" "))
for (j = 0; j <= 7; j++, *byte <<= 1)
bits[j + (i-1)*8] = (* byte) & 128 ? '1' : '0';

printf ("\n%s\n", bits);
}

/* Now we have the extracted bits in the array bits on the form:
seeeeeee  eeeemmmm  mmmmmmmm  mmmmmmmm
mmmmmmmm  mmmmmmmm  mmmmmmmm  mmmmmmmm

Where s = sign bit, e = exponent bits and m = mantissa
if you have a 8 byte double, but it should be 8 bytes.
from here I hope you'll be able to do the rest, if not let me
know and I'll make a whole program for you that converts
both ways. But since it's an assignment I think you should do
some of the work yourself.
*/

Not the solution you were looking for? Getting a personalized solution is easy.

Commented:
Question to virgo21:  My professor told us that on the machine which runs gcc, a real number is represented in the form

ms es mmmmmmmmmmmmmmmmmmmmmmm eeeeeee

where mb = mantissa sign, es = exponent sign, m = mantissa and
e = exponent.  Is this representation different from the 8-byte double representation you were telling me about?

Also, I've written out the first part of the program (converting from HEX to C-Double) and it compiles but unfortunately, it prints out 0 when I type in any hex number.  If possible, may I have the code that will convert both ways (HEX to C-double and back) so that I may compare what I've done wrong in the first part of the program?  Thanks alot for your help -- I greatly appreciate it!

Commented:
I'll work some code out for you until 22/4,
Me and my teacher don't agree with the representation you have showned, you could see the first exponent bit as an sign bit if you want, but here is how you do it:
seeeeeee eeeemmmm mmmmmmmm mmmmmmmm
mmmmmmmm mmmmmmmm mmmmmmmm mmmmmmmm

You calculate how big the exponent is for example:
1000010101 = 2^11+2^5+2^3+2^0 = 4096 + 32 + 8 + 1 = 4147
Now we take the result and subtract 4096 -> 4147 - 4096 = 51
So the exponent is 51.

Remember that when the above program have put the bits into the array then the bytes are all swapped, in order to get the most significant byte of the mantissa first.

I'll work out a little progg so you at least can see how it works on a PC.

##### Thanks for using Experts Exchange.

• View three pieces of content (articles, solutions, posts, and videos)
• Ask the experts questions (counted toward content limit)
• Customize your dashboard and profile