469,636 Members | 1,527 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,636 developers. It's quick & easy.

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

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
2 13330
dinhvy
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
9,208 Expert Mod 8TB
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.

Similar topics

reply views Thread by Ernst Murnleitner | last post: by
2 posts views Thread by Goran | last post: by
54 posts views Thread by Andy | last post: by
10 posts views Thread by Pavils Jurjans | last post: by
5 posts views Thread by Peter Hansen | last post: by
60 posts views Thread by Erick-> | last post: by
7 posts views Thread by Mario M. Mueller | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.