打印多个整数为一个任意长的十进制字符串 [英] Printing multiple integers as one arbitrarily long decimal string

查看:162
本文介绍了打印多个整数为一个任意长的十进制字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有16的64位无符号整数。我一直在谨慎执行操作时喂进它们之间为宜。我可以养活他们进入到所有这些转换成十进制数字的单个字符串的方法,就好像它是一个1024位二进制数?换句话说,是有可能使这将为该重新present一个容量较大的整数整数任意数量的工作方法?

Say I have 16 64-bit unsigned integers. I have been careful to feed the carry as appropriate between them when performing operations. Could I feed them into a method to convert all of them into a single string of decimal digits, as though it was one 1024-bit binary number? In other words, is it possible to make a method that will work for an arbitrary number of integers that represent one larger integer?

我想象,这将是有符号整数更为困难,因为有对处理最显著位。我想这将是最显著整数是有符号整数,剩下的将是无符号,重新present数量的剩余部分。

I imagine that it would be more difficult for signed integers, as there is the most significant bit to deal with. I suppose it would be that the most significant integer would be the signed integer, and the rest would be unsigned, to represent the remaining 'parts' of the number.

(这是半有关<一href=\"http://stackoverflow.com/questions/13878189/understanding-printf-with-integers/13878342\">another问题。)

推荐答案

这是一个有点不清楚。

当然,如

void print_1024bit(uint64_t digits[]);

可以写成做到这一点。但是,如果你的意思是,如果任何标准库的的printf()的功能 - 家庭可以做到这一点,那么我想答案是否定的。

could be written to do this. But if you mean if any of the standard library's printf()-family of functions can do this, then I think the answer is no.

你可能在其他问题所看到的,转换成二进制数到不同基地的核心的的由两个操作:

As you probably saw in the other question, the core of converting a binary number into a different base b is made of two operations:


  • 模的的,要弄清楚目前至少有显著数字

  • 除以的,一旦它被生成以消除数字

  • Modulo b, to figure out the current least significant digit
  • Division by b, to remove that digit once it's been generated

当施加直到数为0,这产生在相反的顺序的所有数字。

When applied until the number is 0, this generates all the digits in reverse order.

所以,你需要除以10为您的1024位数字,实现模数10,

So, you need to implement "modulo 10" and "divide by 10" for your 1024-bit number.

例如,考虑小数4711,这是我们要转换为八进制数只是这个例子:

For instance, consider the number decimal 4711, which we want to convert to octal just for this example:


  1. 4711%8是7,所以最右边的数字为 7

  2. 八分之四千七百十一588

  3. 588%,8 4,下一个数字就是 4

  4. 八分之五百八十八为73

  5. 73%,8 1

  6. 八分之七十三是9

  7. 9%,8 1

  8. 8月8日为1

  9. 1%,8 1

  10. 1月8日0时,我们就大功告成了。

所以,从底部读粗体数字和,迈向最右边的数字,我们认为4711 10 = 11147 8 。您可以使用计算器来验证这一点,或者只是相信我。 :)

So, reading the bold digits from the bottom and up towards the right-most digits, we conclude that 471110 = 111478. You can use a calculator to verify this, or just trust me. :)

这篇关于打印多个整数为一个任意长的十进制字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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