开发者

convert double value to binary value

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


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.


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.


A decade late but hopefully this will help someone:

// Converts a double value to a string in base 2 for display.
// Example: 123.5 --> "0:10000000101:1110111000000000000000000000000000000000000000000000"
// Created by Ryan S. White in 2020, Released under the MIT license.
string DoubleToBinaryString(double val)
{
    long v = BitConverter.DoubleToInt64Bits(val);
    string binary = Convert.ToString(v, 2);
    return binary.PadLeft(64, '0').Insert(12, ":").Insert(1, ":");
}


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]);


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.


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++.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜