473,406 Members | 2,894 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 13675
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

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

Similar topics

0
by: Ernst Murnleitner | last post by:
Dear readers, Maybe someone can help: I need to exchange data with a PLC. My computer (Linux on intel) uses the intel format (little endian), the other device uses the motorola format (big...
2
by: Goran | last post by:
Hi! I need to convert from a unsigned char array to a float. I don't think i get the right results in the program below. unsigned char array1 = { 0xde, 0xc2, 0x44, 0x23}; //I'm not sure in...
54
by: Andy | last post by:
Hi, I don't know if this is the correct group to post this, but when I multiply a huge floating point value by a really small (non-zero) floating point value, I get 0 (zero) for the result. This...
10
by: Pavils Jurjans | last post by:
Hallo, It is know issue that due to the fact that computer has to store the real numbers in limited set of bytes, thus causing a minor imprecision from the decimal value that likely was stored....
5
by: Peter Hansen | last post by:
I'm investigating a puzzling problem involving an attempt to generate a constant containing an (IEEE 754) "infinity" value. (I understand that special float values are a "platform-dependent...
60
by: Erick-> | last post by:
hi all... I've readed some lines about the difference between float and double data types... but, in the real world, which is the best? when should we use float or double?? thanks Erick
28
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
1
by: Wayne Shu | last post by:
Hei everyone: Just see the output of the following program #include <iostream> #include <cstdlib> #include <limits> int main() { std::cout << "minimum exponent of double: " <<
7
by: Mario M. Mueller | last post by:
Hi, I have a binary file containing 3 byte float values (big endian). How can I read them into python? The struct module does not work, since it expects 4 byte floats. Any hints? Mario
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.