Solved

Real number to Hex conversion

Posted on 1997-04-17
6
545 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
  • 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now