i want to convert real float number to 32-bit IEEE binary format in C++ ? plz help me in finding the codes as fast as u can ]
Example
for input -6.5, the output should be.
1 10000001 101000000000000000000000
0 1 8 9 31
S = 1
E = 129
F = 1.101
:(
here is simple code C:
#include "stdio.h"
#include "conio.h"
char* char2Binary(unsigned char ch)
{
int temp = (int)ch;
char* arrayChar = new char[8];
int sodu = 0;
int i = 0;
for(i=0; i<8; i++)
arrayChar[i] = '0';
i = 7;
do
{
sodu = temp % 2;
temp = temp / 2;
switch(sodu)
{
case 1:
{
arrayChar[i] = '1';
break;
}
case 0:
{
arrayChar[i] = '0';
break;
}
}
i--;
} while(temp != 0);
return arrayChar;
}
void main()
{
float f = 12.4f;
printf("Please type a float number: ");
scanf("%f", &f);
char* pByte = (char*)&f;
printf("Binary form is: \n");
for(int i=3; i >= 0 ; i--)
{
unsigned char k = *(pByte + i);
//printf("%d \n", k);
char* a = new char[8];
a = char2Binary(k);
for(int j=0; j<8; j++)
printf("%c",*(a+j));
printf(" ");
}
getch();
}