This may be trivial for most of you, but I've been scratching my head as to how to convert real numbers (ie 5.375) to binary. Actually, I'm trying to convert the real number to 32-bit IEEE binary format, but I'm just working with trying to convert it to binary for now.

I can get the part before the decimal to work out (ie the 5), but I can't seem to get the .375 conversion. Here's what I've come up with so far... any help will be much appreciated!! (horrible code I know, please bear with me!)

#include <stdio.h>
#include <stdlib.h>
#include <iostream.h>
#include <string.h>
#include <math.h>


void main(void)
{
	float value=0, tempvalue=0;
	int flag =0;
	float decright=0;
	int decleft=0;

	printf("Enter a real number: ");
	scanf("%f", &value);

	if (value < 0)
		flag = 1;

	value=tempvalue=fabs(value);

	decleft=(int)tempvalue;
	decright=value-decleft;	// separate right & left side of decimal

	// convert left side to decimal to binary

	int  j=0, k = 0, n = 0;
        int  remain;
        char temp[80];
	char binleft[80];
	char binright[80];
	float track;
  
    do
    {
		remain = decleft % 2;
        
		if (remain == 1)
			temp[k++] = '1';
		else
			temp[k++] = '0';

		decleft = decleft / 2;

    } while (decleft != 0);
	
    while (k >= 0)
    {
            binleft[n++] = temp[--k];   // reverse 
    }

    binleft[n-1] = 0;             // add NULL

	printf("\n The binary value of is %s \n",binleft);

	// convert right side to binary

	do
	{
		track = decright*2;
		
		if ( (track-1)<0 )
		{
			decright = track;
			binright[j] = '0';
			j = ++j;
		}

		else
		{
			decright = track-1;
			binright[j] = '1';
			j = ++j;
		}
	} while (decright > 1);

	printf("\n binary of right is %s \n", binright);

}

Would some adaptations of this be helpful?

#include <stdio.h>
#include <limits.h>

char *bits_uchar ( char *dest, unsigned char value )
{
   char *start = dest;
   unsigned char bit;
   for ( bit = 1 << (CHAR_BIT - 1); bit > 0; bit >>= 1 )
   {
      *dest++ = value & bit ? '1' : '0';
   }
   *dest = 0;
   return start;
}

char *bits_block ( char *dest, const void *object, size_t size )
{
   char *start = dest;
   const unsigned char *byte = object;
   for ( byte += size - 1; size-- > 0; --byte )
   {
      bits_uchar ( dest, *byte );
      dest += CHAR_BIT;
      *dest++ = size > 0 ? '-' : 0;
   }
   return start;
}

int main(void)
{
   char result [ 80 ];
   float value = 5.375;
   printf ( "%g = \"%s\"\n", value, 
            bits_block ( result, &value, sizeof value ) );
   return 0;
}

/* my output
5.375 = "01000000-10101100-00000000-00000000"
*/

http://babbage.cs.qc.edu/courses/cs341/IEEE-754.html

When I tried it I get a type cast error ?

Conversion from 'void*' to pointer to non-'void' requires an explicit cast

thanks so much.

nevermind, I figured out the typecast.. silly me. The code is working like a charm, thank you so much!

If it's not too much trouble, could you also explain to me what is going on in your code? I really want to understand this and learn. In particular, just the part quoted below. How does this convert the value to binary??


char *bits_uchar ( char *dest, unsigned char value )
{
char *start = dest;
unsigned char bit;
for ( bit = 1 << (CHAR_BIT - 1); bit > 0; bit >>= 1 )
{
*dest++ = value & bit ? '1' : '0';
}
*dest = 0;
return start;
}

If it's not too much trouble, could you also explain to me what is going on in your code? I really want to understand this and learn. In particular, just the part quoted below. How does this convert the value to binary??

Basically it loops through the bits of the object representation.

Did you look at Part 1 and/or Part 3? If you have specific questions or comments, I can try to improve these snippets. (I'm too familiar with them to remember what needs better explanations.)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.