Click here to Skip to main content
15,887,776 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi i am a beginner to c programming. i want to know how to convert a float to binary. please explain with example.
Posted
Comments
Richard MacCutchan 5-Nov-13 7:16am    
What do you mean by convert; convert to an integer, or convert to a printable format?
Stefan_Lang 5-Nov-13 11:04am    
You seem to be confusing two (mostly) unrelated concepts - or you haven't expressed your question well:

"float" is a data type used to describe the kind of value a variable of that type contains: it is a number that may contain a fractional part and/or an exponent. Examples would be:
5, -6.23, 3.1415, -0.739e-11

"binary" is a specific representation of a numeric value in the form of a string of '0's and '1's. E. g. you can represent the number 5 by writing it in its binary representation as "%101" (where the leading '%' indicates a number in binary notation as per the C/C++ standard)

Anyway, the representation of a number is something different than a type. You can print a binary representation of a floating point number, but doing so does not convert or otherwise affect the original value.
Hamed Moezzi Azimi 5-Nov-13 12:57pm    
Your answer is in here:
http://stackoverflow.com/questions/3954498/how-to-convert-float-number-to-binary

There is no conversion from float to binary since float variables are already stored in the binary form stated by the IEEE_754 standard[^].

If you need to show how a float is stored in a human friendly format, then you may use, for instance, the following code:
C
#include <stdio.h>


void out_float(float f)
{
  size_t size = sizeof(f);
  unsigned char * p = (unsigned char *) &f;
  p += size-1;
  while (size--)
  {
    int n;
    for (n=0; n<8; n++)
    {
      putchar('0' + (*p & 128 ? 1 : 0));
      *p <<= 1;
    }
    p--;
  }
}


int main()
{
  float f = -118.625f;

  out_float(f);
  putchar('\n');
  return 0;
}
 
Share this answer
 
v2
Binary? You mean you want to read the bytes of it? You can use a union for that. Every field in a union represents the same data in memory.
C#
union floatUnion_t {
    float f;
    char bytes[sizeof float]
} floatUnion1;

With this union you can read/write f as a float and read/write the bytes of it as well.

Good luck!
 
Share this answer
 
There is a function that convert int* into binary.
So get your float's address & cast it to int* then convert it into binary by inbuilt function
C++
float MyFloat = 10.5;
float *p;
p = &MyFloat;
convert_int_to_binary_string(*((int*)p));


This Might work for you.

OR one can refer this too.
this may be help you.
http://stackoverflow.com/questions/12868444/convert-float-to-binary-in-c[^]
http://www.cquestions.com/2011/07/c-program-for-fractional-decimal-to.html[^]

Hope This Help
------------------
Pratik Bhuva
 
Share this answer
 
v2
Comments
nv3 8-Nov-13 7:37am    
This is doing actually the opposite: It converts a float (which is stored in binary format) into a text representation.
#include <iostream>
#include <bitset>

int main()
{
union
{
float input; // assumes sizeof(float) == sizeof(int)
int output;
} data;

data.input = 2.25125;

std::bitset<sizeof(float)> bits(data.output);


std::cout << bits << std::endl;

// or

std::cout << "BIT 4: " << bits[4] << std::endl;
std::cout << "BIT 7: " << bits[7] << std::endl;
}
 
Share this answer
 
Hi,

Numbers could be change to binary through dividing by 2 until it becomes less than 2;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900