473,564 Members | 2,758 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

1 New Member
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 101000000000000 000000000
0 1 8 9 31

S = 1
E = 129
F = 1.101
:(
Oct 31 '05 #1
2 13689
dinhvy
1 New Member
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 101000000000000 000000000
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(uns igned 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 Recognized Expert Moderator Expert
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
2375
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 endian). So I have to change the order of the bytes for "int". But for "float":
2
32285
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 what order the data is stored so i try both ways. unsigned char array2 = { 0x23, 0x44, 0xc2, 0xde}; float *pfloat1, *pfloat2;
54
8325
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 creates a big hole in a 32-bit timer routine I wrote. Questions. 1. Why does this happen? 2. Is there C macros/functions I can call to tell me...
10
2680
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. I don't know, how dotnet framework stored doubles, but it's certain, that if I store, say, 1.234567 in some real variable, it is stored in memory...
5
4178
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 accident" etc...) The issue appears possibly to point to a bug in the Python compiler, with it producing inconsistent results. I'm using "Python 2.4.2...
60
7174
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
5841
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places? ----------------------------------------------------------------------- When formatting money for example, to format 6.57634 to 6.58, 6.5 to 6.50, and 6 to 6.00? Rounding of x.xx5 is...
1
2813
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
7197
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
7665
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7888
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7642
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7950
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6255
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5484
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1200
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.