从二进制转换非常大的数字为十进制并打印 [英] Convert really big number from binary to decimal and print it

查看:166
本文介绍了从二进制转换非常大的数字为十进制并打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何二进制十进制转换。我知道至少有2种方法:表和力量; - )

I know how to convert binary to decimal. I know at least 2 methods: table and power ;-)

我要二进制转换为十进制并打印此十进制。此外,我不关心这个'小数';我只想打印出来。

I want to convert binary to decimal and print this decimal. Moreover, I'm not interested in this `decimal'; I want just to print it.

但是,正如我上面写的,我知道只有2种方法二进制转换为十进制和他们都需要增加。所以,我计算二进制的1或0的某个值,并将其添加到记住的价值。这是一个薄的地方。我有一个非常,非常大的数字(1和64个零)。虽然转换我需要将一些中间结果一些变量。在C中,我有一个'廉政'类型,这是4个字节,而不是只超过10 ^ 11。

But, as I wrote above, I know only 2 methods to convert binary to decimal and both of them required addition. So, I'm computing some value for 1 or 0 in binary and add it to the remembered value. This is a thin place. I have a really-really big number (1 and 64 zeros). While converting I need to place some intermediate result in some 'variable'. In C, I have an `int' type, which is 4 bytes only and not more than 10^11.

所以,我没有足够的内存来存储intermedite结果,而从二进制到十进制转换。正如我上面写的,我不感兴趣的是十进制,我只是想打印结果。但是,我没有看到任何其他方式来解决这个问题;-(是否有任何解决方案,从二进制只打印?

So, I don't have enough memory to store intermedite result while converting from binary to decimal. As I wrote above, I'm not interested in THAT decimal, I just want to print the result. But, I don't see any other ways to solve it ;-( Is there any solution to "just print" from binary?

或者,也许,我应该使用中间再presentation类似BCD(二进制codeD十进制)?我真的不想用这个,因为它不是那么跨平台(Intel的处理器有一个内置功能,但对其他我需要写自己的实现)。

Or, maybe, I should use something like BCD (Binary Coded Decimal) for intermediate representation? I really don't want to use this, 'cause it is not so cross-platform (Intel's processors have a built-in feature, but for other I'll need to write own implementation).

我会很高兴听到你的看法。感谢您的耐心。

I would glad to hear your thoughts. Thanks for patience.

语言:C

推荐答案

最大标准的整体数据类型为无符号长long int类型 - 我的系统中(32位Linux上86)有范围0 - 1.8 * 10 ^ 20这是不够的,你,所以你需要创建自己的类型(结构或数组),写基本的数学(基本上你只需要为该类型的加法)

Biggest standard integral data type is unsigned long long int - on my system (32-bit Linux on x86) it has range 0 - 1.8*10^20 which is not enough for you, so you need to create your own type (struct or array) and write basic math (basically you just need an addition) for that type.

如果我是你(和内存是不是一个问题),我会用一个数组 - 每个十进制数字而不是BCD一个字节。 BCD更加紧凑,因为它存储每个字节2十进制数字,但是你需要把更多的精力用单独高和低半字节工作。

If I were you (and memory is not an issue), I'd use an array - one byte per decimal digit rather then BCD. BCD is more compact as it stores 2 decimal digits per byte but you need to put much more effort working with high and low nibbles separately.

和打印时,只需添加 0 (字符,而不是数字),阵列的每个字节,你会得到一个打印字符串。

And to print you just add '0' (character, not digit) to every byte of your array and you get a printable string.

这篇关于从二进制转换非常大的数字为十进制并打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆