Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 605
  • Last Modified:

Real number to Hex conversion

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?
0
PFabregas
Asked:
PFabregas
  • 3
  • 2
1 Solution
 
warren_daleCommented:
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?
0
 
PFabregasAuthor 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.
0
 
PFabregasAuthor Commented:
Edited text of question
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
virgo21Commented:
/* 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.
*/

0
 
PFabregasAuthor 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!

0
 
virgo21Commented:
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.


0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now