views:

513

answers:

5

How can i convert double value to binary value.

i have some value like this below 125252525235558554452221545332224587265 i want to convert this to binary format..so i am keeping it in double and then trying to convert to binary (1 & 0's).. i am using C#.net

A: 

If you mean you want to do it yourself, then this is not a programming question.

If you want to make a computer do it, the easiest way is to use a floating point input routine and then display the result in its hex form. In C++:

double f = atof ("5.5555555555556E18");
unsigned char  *b = (unsigned char *) &f;
for (int j = 0; j < 8;  ++j)
    printf (" %02x", b [j]);
wallyk
You may want to mention that the 8 is for a very specific data type. I don't think IEEE754 64-bit is mandated by the C standard.
paxdiablo
+2  A: 

Well, you haven't specified a platform or what sort of binary value you're interested in, but in .NET there's BitConverter.DoubleToInt64Bits which lets you get at the IEEE 754 bits making up the value very easily.

In Java there's Double.doubleToLongBits which does the same thing.

Note that if you have a value such as "125252525235558554452221545332224587265" then you've got more information than a double can store accurately in the first place.

Jon Skeet
A: 

A double value already IS a binary value. It is just a matter of the representation that you wish it to have. In a programming language when you call it a double, then the language that you use will interpret it in one way. If you happen to call the same chunk of memory an int, then it is not the same number.

So it depends what you really want... If you need to write it to disk or to network, then you need to think about BigEndian/LittleEndian.

Cine
raj
BTW, that number is 127 bits long so can only be approximated (poorly) with a double.
Rick Regan
+3  A: 

In C, you can do it for instance this way, which is a classic use of the union construct:

int i;
union {
 double x;
 unsigned char byte[sizeof (double)];
} converter;

converter.x = 5.5555555555556e18;

for(i = 0; i < sizeof converter.byte; i++)
  printf("%02x", converter.byte[i]);

If you stick this in a main() and run it, it might print something like this:

~/src> gcc -o floatbits floatbits.c
~/src> ./floatbits
ba b5 f6 15 53 46 d3 43

Note though that this, of course, is platform-dependent in its endianness. The above is from a Linux system running on a Sempron CPU, i.e. it's little endian.

unwind
A: 

For these huge numbers (who cannot be presented accurately using a double) you need to use some specialized class to hold the information needed.

C# provides the Decimal class:

The Decimal value type represents decimal numbers ranging from positive 79,228,162,514,264,337,593,543,950,335 to negative 79,228,162,514,264,337,593,543,950,335. The Decimal value type is appropriate for financial calculations requiring large numbers of significant integral and fractional digits and no round-off errors. The Decimal type does not eliminate the need for rounding. Rather, it minimizes errors due to rounding. For example, the following code produces a result of 0.9999999999999999999999999999 rather than 1.

If you need bigger precision than this, you need to make your own class I guess. There is one here for ints: http://sourceforge.net/projects/cpp-bigint/ although it seems to be for c++.

Cine