473,763 Members | 6,401 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

convert float to binary

4 New Member
how to convert * WITHOUT USING ANY FUNCTION * a float number to binary?
at least if someone can explain me how to do it it would be better then giving me the code itself.
Nov 15 '06 #1
7 46557
Banfa
9,065 Recognized Expert Moderator Expert
You are going to have to explain what you want to do better than that.

A float is already held in a binary format.
Nov 15 '06 #2
ayaniv
4 New Member
i will give you an example in converting an integer to binary:
the number 245 = 11110101 in binary notation.
i got the answer by algorithm that translate 245 to binary.

i want to write an algorithm for float numbers.
as i will put 12.34 and get its binary notation.

that's it.
Nov 15 '06 #3
Banfa
9,065 Recognized Expert Moderator Expert
i will give you an example in converting an integer to binary:
the number 245 = 11110101 in binary notation.
i got the answer by algorithm that translate 245 to binary.

i want to write an algorithm for float numbers.
as i will put 12.34 and get its binary notation.

that's it.
But ints and floats are 2 completely different kettle of fish. Your int (value 245) is actually held in memory in the binary notation you give 11110101.

However the binary notation of the float 12.34 is

1100.0101011100 001010001111010 111000010100011 11010111

But 12.34 in a double variable is held in memory as

10101110 01000111 11100001 01111010
00010100 10101110 00101000 01000000

and 12.34 in a float variable is held in memory as

10100100 01110000 01000101 01000001

So the question is which one are you trying to calculate?
Nov 15 '06 #4
ayaniv
4 New Member
i guess it's the last one:
10100100 01110000 01000101 01000001

if you please can tell me how you get to binary notation from 12.34
so i can write the correct algorithm to do it.

thanx
Nov 15 '06 #5
horace1
1,510 Recognized Expert Top Contributor
i guess it's the last one:
10100100 01110000 01000101 01000001

if you please can tell me how you get to binary notation from 12.34
so i can write the correct algorithm to do it.

thanx
Would be worth having a look at URL
http://en.wikipedia.or g/wiki/IEEE_754

which describes the IEEE 754 floating point standard
Nov 15 '06 #6
Banfa
9,065 Recognized Expert Moderator Expert
i guess it's the last one:
10100100 01110000 01000101 01000001

if you please can tell me how you get to binary notation from 12.34
so i can write the correct algorithm to do it.
As pseudo code it is

Expand|Select|Wrap|Line Numbers
  1. DECLARE FLOAT f
  2. SET f = 12.34
  3. DECLARE BYTE POINTER p
  4. SET p = THE ADDRESS OF f
  5.  
  6. FOR EACH BYTE IN F
  7.     PRINT THE BYTE p POINTS TO IN BINARY
  8.     SET p = p + 1
  9. END FOR
  10.  
Nov 15 '06 #7
horace1
1,510 Recognized Expert Top Contributor
i guess it's the last one:
10100100 01110000 01000101 01000001

if you please can tell me how you get to binary notation from 12.34
so i can write the correct algorithm to do it.

thanx
printing integers in binary is easy so you could have a union, e.g.
Expand|Select|Wrap|Line Numbers
  1. typedef union  {
  2.     float f;
  3.     int i;
  4. } Number;
  5.  
assign the floating point value to f and then print i in binary

you would need to check that int and float were the same size using the sizeof() operator
Nov 16 '06 #8

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

Similar topics

4
8351
by: Jean-Baptiste PERIN | last post by:
I read 4 bytes from a binary file. These bytes represent a floating point number (mantisse exponent form) How can I get a float from these bytes ?
4
23168
by: jaijai_kumar | last post by:
Select Cast('100.1234' as float) give me the result 100.1234 Now when I convert it back to char I want exactly 100.1234 Select Convert(char(100),Cast('100.1234' as float)) Gives me 100.123 (Here I was expecting 100.1234) When I do Select STR(Cast('100.1234' as float),25,4) I get back the result as 100.1234
2
13701
by: respect | last post by:
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
4
10718
by: music4 | last post by:
Greetings, I psinfo_t struct, pr_pctcpu is ushort_t type with comment: "pr_pctcpu is 16 bit binary fractions in the range 0.0 to 1.0 with the binary point to the right of the high-orfer bit (1.0=0x8000)." My question is how to convert it to a float? Thanks in advance! Evan
2
6006
by: bilbo2000 | last post by:
I am trying to read in a binary file which has the data stored as 32 bit float. I have been trying to figure out how to read this in as 32 bit but everytime I try to read it in the best I can do is a 8 bit integer. I was able to do this using a scripting language php, and found that the data is stored in little endian format, so I would read 4 characters in and reverse the string and declare it a float and the number would come out...
35
2218
by: bala.pandu | last post by:
Hello Everyone, When i am assigning a float value and try to print the same, i found the value stored is not the exact value which i gave. int main() { float f1; printf("Enter a float value : \n"); scanf("%f",&f1);
7
7250
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
7
6290
by: chelle2100 | last post by:
hi... i need to write a converter to convert the text file to a binary file with the following file format... CharacterCount:Unsigned Char(This stores the number of characters that is in the file) Name:Char(An array of 256 char that stores the name of the character. You will need to prompt the user if the name given in the text file is longer than the maximum length it can hold) Weapon:Unsigned Short(Uses a single 16bit data type to store...
4
22301
by: Mason | last post by:
I have tried and tried... I'd like to read in a binary file, convert it's 4 byte values into floats, and then save as a .txt file. This works from the command line (import struct); In : f = open("test2.pc0", "rb") In : tagData = f.read(4) In : tagData
0
9563
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10144
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9937
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9822
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7366
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3522
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2793
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.