Floating point to hexadecimal, arranged by the most "important" bits

This is a discussion on Floating point to hexadecimal, arranged by the most "important" bits within the C Programming forums, part of the General Programming Boards category; Hi, I have this assignment to do, in which the program asks the user for floating point input, after getting ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    9

    Floating point to hexadecimal, arranged by the most "important" bits

    Hi, I have this assignment to do, in which the program asks the user for floating point input, after getting this the floating point is printed as a hexadecimal bit by bit, arranging so that the most important bit is first etc.

    This is what I get, it seems to work quite properly on presenting the hexadecimal sometimes. Sometimes it just prints a whole lot of F's even though they really should not be there. Could someone please guide me to the righ direction?

    Clearly I'm not arranging these yet, first I'd like it to present the data correctly.

    Code:
    #include <stdio.h>
    
    int main (void)
    {
    	int i;
    	double nbr;
    	char *ptr;
    
    	printf("Type a floating point:\n");
    	scanf("%lf", &nbr);
    	ptr = &nbr;
    	printf("\Number %lf as a hexadecimal is \n", nbr);
    
    	for(i=0;i<sizeof(double);i++)
    		printf("%X ", *ptr++);
    
    	return 0;
    }

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    If you're printing a number bit by bit, what is the point of doing it in hexadecimal? Do you mean byte by byte?

    You're going to have a number of issues here. For one, floating point representation in binary is usually (like always?) not the same as regular integer binary representations, which means you're not going to get what you might expect. Example: If you enter in 15, and you expect to see F, you probably won't see it.

    Another issue is endianess. If you enter the 2 byte hexadecimal value AABB, on some machines it will appear in memory as BBAA. Reading it sequentially byte by byte that way might cause this type of seemingly weird behavior.

    Are you sure your assignment wants a floating point number? And what format do they actually want it printed back in? Just whatever the comes back in whatever format?

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    9
    The assignment was to "create a program which asks for double and prints it byte by byte as a hex. The bytes should be arranged so that the most important byte comes first." It is just a school assignment, to be more familiar with floating points I guess.

    We did the same with integers excluding the arrangment of bytes at school, so I am sure that this needs to be double.

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Depending on your architecture, you may have to do some bit shifting then if you want the raw bytes in the exact order specified.

    Otherwise, if he wants it broken down so that 15.15 comes out as F.F, then that's different.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    9
    Depending on your architecture, you may have to do some bit shifting then if you want the raw bytes in the exact order specified.

    Otherwise, if he wants it broken down so that 15.15 comes out as F.F, then that's different.
    The arrangement is not the biggest problem now, I'll see into that later on. The problem is how my code (as pasted above) represents the number as a hex right on some occasions. Either that or I don't understand the concept.

    As I said, we did the same thing for integers while at school, so the teacher wants it as I explained.

    The program should work like this one: http://babbage.cs.qc.edu/IEEE-754/Decimal.html


    Edit: I found a solution, pointer should be unsigned char
    Last edited by Slither; 12-02-2007 at 04:04 AM.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,499
    Well first you have to realise that floats/doubles are composed of 'sign', 'mantissa' and 'exponent' groups of bits.

    Typically, you memcpy the float/double into say an unsigned long, then use bitwise ops to extract individual bits.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    make char a pointer to unsigned char to print hex
    Code:
    #include <stdio.h>
    
    int main (void)
    {
    	int i;
    	double nbr;
    	unsigned char *ptr;
    
    	printf("Type a floating point:\n");
    	scanf("&#37;lf", &nbr);
    	ptr = (unsigned char *)&nbr;
    	printf("\nNumber %lf as a hexadecimal is \n", nbr);
    
    	for(i=0;i<sizeof(double);i++)
    		printf("%X ", *ptr++);
    
    	return 0;
    }
    Kurt

  8. #8
    Registered User
    Join Date
    Oct 2007
    Posts
    9
    I got it.. The final code (including the arrangement is as follows):

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main (void)
    {
    	int i;
    	double nbr;
    	unsigned char *ptr;
    	printf("Input a floating point:\n");
    	scanf("%lf", &nbr);
    	ptr = &nbr;
    	ptr = (ptr+7);
    
    	printf("\Number %lf as a hexadecimal is \n", nbr);
    	for(i=0;i<sizeof(double);i++){
    		printf("%X ", *ptr--);
    	}
    	return 0;
    }
    It seems to work ok.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. checking for floating point number
    By ssharish2005 in forum C Programming
    Replies: 6
    Last Post: 10-18-2005, 08:14 PM
  2. Floating point #'s, why so much talk about it?
    By scuzzo84 in forum C Programming
    Replies: 5
    Last Post: 09-20-2005, 04:29 PM
  3. Floating point exceptions
    By Boris in forum C++ Programming
    Replies: 8
    Last Post: 11-19-2001, 07:32 AM
  4. Floating point faster than fixed-point
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 11-08-2001, 10:34 PM
  5. Replies: 2
    Last Post: 09-10-2001, 12:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21