开发者

Decimal to Binary conversion - I need Help [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Decimal to Binary conversion

I need to convert a 20digits decimal to binary using C programming. How do I go about it. Really, I am finding it hard. What buffer will I create, because it will be very large, even the calculator can't compute converting 20 digits t开发者_运维百科o Binary.

I need suggestions, links and possibly sample codes.

Thanks.


Do you need to convert a decimal string to a binary string or to a value?

Rule of thumb 10^3 ~= 2^10, therefore 10^20 ~= 2^70 > 64 bits (67 to be accurate).

==> A 64bit integer will not not be enough. You can you a structure with 2 64bit integers (long long in C) or even a 8bit byte for the upper part and 64 for the lower part. Make sure the lower part is unsigned.

You will need to write code that checks for overflow on lower part and increases upper part when this happens. You will also need to use the long division algorithm once you cross the 64bit line.


What about using a library for extended precision arithmetic? try to give a look at http://gmplib.org/


I don't know if you are trying to convert a string of numerical characters into a really big int, or a really big int into a string of 1s and 0s... but in general, you'll be doing something like this:

for (i = 0; i < digits; i++)
{
    bit[i] = (big_number>>i) & 1;
    // or, for the other way around
    // big_number |= (bit[i] << i);
}

the main problem is that there is no builtin type that can store "big_number". So you'll probably be doing it more like this...

uint8_t big_number[10]; // the big number is stored in 10 bytes.
// (uint8_t is just "unsigned char")
for (block = 0; block < 10; block++)
{
    for (i = 0; i < 8; i++)
    {
        bit[block*8 + i] = (big_number[block]>>i) & 1;
    }
}

[edit]

To read an string of numerical characters into an int (without using scanf, or atoi, etc), I would do something like this:

// Supposing I have something like char token[]="23563344324467434533";
int n = strlen(token); // number of digits.
big_number = 0;
for (int i = 0; i < n; i++)
{
    big_number += (token[i] - '0') * pow(10, n-i-1);
}

That will work for reading the number, but again, the problem with this is that there is no built-in type to store big_number. You could use a float or a double, and that would get the magnitude of the number correct, but the last few decimal places would be rounded off. If you need perfect precision, you will have to use an arbitrary-precision integer. A google search turns up a few possible libraries to use for that; but I don't have much personal experience with those libraries, so I won't make a recommendation.

Really though, the data type you use depends on what you want to do with the data afterwards. Maybe you need an arbitrary-precision integer, or maybe a double would be exactly what you need; or maybe you can write your own basic data type using the technique I outlined with the blocks of uint8_t, or maybe you're better off just leaving it as a string!

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜