0

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);
}
```