By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,397 Members | 2,515 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,397 IT Pros & Developers. It's quick & easy.

i want to convert real float number to 32bit IEEE binary format in C++ ? plz help me

P: 1
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
:(
Oct 31 '05 #1
Share this Question
Share on Google+
2 Replies


P: 1
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();
}
Jun 16 '07 #2

weaknessforcats
Expert Mod 5K+
P: 9,197
Use a float.

Expand|Select|Wrap|Line Numbers
  1. float f = -6.5;
  2.  
That is 32-bit IEEE binary.
Jun 17 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.