Solved

Real number to Hex conversion

Posted on 1997-04-17
6
564 Views
Last Modified: 2011-10-03
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
Comment
Question by:PFabregas
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 1

Expert Comment

by:warren_dale
ID: 1249937
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
 

Author Comment

by:PFabregas
ID: 1249938
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
 

Author Comment

by:PFabregas
ID: 1249939
Edited text of question
0
Industry Leaders: 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!

 

Accepted Solution

by:
virgo21 earned 100 total points
ID: 1249940
/* 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
 

Author Comment

by:PFabregas
ID: 1249941
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
 

Expert Comment

by:virgo21
ID: 1249942
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

Industry Leaders: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question