将非常大的数字从二进制转换为十进制并打印

发布于 2024-07-23 12:12:22 字数 568 浏览 5 评论 0原文

我知道如何将二进制转换为十进制。 我至少知道两种方法:表和幂;-)

我想将二进制转换为十进制并打印这个十进制。 而且,我对这个“小数”不感兴趣; 我只想打印它。

但是,正如我上面所写,我只知道两种将二进制转换为十进制的方法,并且它们都需要加法。 因此,我正在计算二进制 1 或 0 的某个值,并将其添加到记住的值中。 这是一个薄弱的地方。 我有一个非常非常大的数字(1 和 64 个零)。 在转换时,我需要将一些中间结果放入一些“变量”中。 在 C 中,我有一个“int”类型,它只有 4 个字节,不超过 10^11。

因此,在从二进制转换为十进制时,我没有足够的内存来存储中间结果。 正如我上面所写,我对那个小数不感兴趣,我只想打印结果。 但是,我没有看到任何其他方法来解决它;-( 有没有任何解决方案可以从二进制“仅打印”?

或者,也许我应该使用 BCD(二进制编码的十进制)之类的东西来进行中间表示?我真的不知道不想使用这个,因为它不是那么跨平台(英特尔的处理器有一个内置功能,但对于其他功能,我需要编写自己的实现),

我很高兴听到您的想法,谢谢您的耐心。 .

语言:C

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.

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.

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?

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.

Language: C.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

執念 2024-07-30 12:12:22

我强烈建议使用诸如 GMP(GNU 多精度库)之类的库。 您可以将 mpz_t 数据类型用于大整数、各种 导入/导出例程 将数据导入 mpz_t,然后使用 mpz_out_str() 以 10 为基数打印出来。

I highly recommend using a library such as GMP (GNU multiprecision library). You can use the mpz_t data type for large integers, the various import/export routines to get your data into an mpz_t, and then use mpz_out_str() to print it out in base 10.

梦初启 2024-07-30 12:12:22

最大的标准整数数据类型是 unsigned long long int - 在我的系统(x86 上的 32 位 Linux)上,它的范围为 0 - 1.8*10^20,这对你来说还不够,所以你需要创建您自己的类型(结构体或数组)并为该类型编写基本数学(基本上您只需要添加)。

如果我是你(并且内存不是问题),我会使用数组 - 每个十进制数字一个字节,而不是 BCD。 BCD 更紧凑,因为它每个字节存储 2 个十进制数字,但您需要花费更多的精力分别处理高半字节和低半字节。

要打印,您只需将 '0' (字符,而不是数字)添加到数组的每个字节中,您就会得到一个可打印的字符串。

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.

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.

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

泪之魂 2024-07-30 12:12:22

好吧,当从二进制转换为十进制时,您实际上并不需要同时使用所有二进制位。 您只需要当前正在计算其幂的位,并且可能需要一个双变量来保存结果。
您可以将二进制值放入一个数组中,比如说 i[64],迭代它,根据其位置获取幂,然后继续将其添加到双精度数中。

Well, when converting from binary to decimal, you really don't need ALL the binary bits at the same time. You just need the bits you are currently calculating the power of and probably a double variable to hold the results.
You could put the binary value in an array, lets say i[64], iterate through it, get the power depending on its position and keep adding it to the double.

十六岁半 2024-07-30 12:12:22

转换为十进制实际上意味着计算十的每一次幂,那么为什么不将它们存储在字节数组中呢? 然后打印只是循环遍历数组。

Converting to decimal really means calculating each power of ten, so why not just store these in an array of bytes? Then printing is just looping through the array.

握住你手 2024-07-30 12:12:22

您不能为 5 个 int 分配内存,并将您的数字存储在数组的开头吗? 然后以 int 大小的块手动迭代数组。 也许是这样的:

int* big = new int[5];
*big = <my big number>;

Couldn't you allocate memory for, say, 5 int's, and store your number at the beginning of the array? Then manually iterate over the array in int-sized chunks. Perhaps something like:

int* big = new int[5];
*big = <my big number>;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文