以 10 为基数打印以 4294967296 为基数的整数

发布于 2024-12-11 12:52:47 字数 152 浏览 6 评论 0原文

我在 C++ 中有一个 32 位整数向量(可变大小,连续内存;就像 C 数组),代表基数 4294967296 中的数字。我想以基数 10 打印它。

这些数字可能非常大并接管几兆字节的内存。

就性能而言,最好的方法是什么?我可以使用 GMP 来做到这一点吗?

I have in C++ a vector of 32 bits integers (variable size, continous memory; like a C-array), representing a number in base 4294967296. I would like to print it in base 10.

These numbers can be extremely big and take over a few megabytes of memory.

What would be the best way to do this in terms of performance? Can I use GMP to do this?

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

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

发布评论

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

评论(1

故事还在继续 2024-12-18 12:52:47

是的,您可以为此使用 GMP。您要查找的函数是 mpn_get_str

http://gmplib.org/manual/Low_002dlevel-Functions.html#Low_002dlevel-Functions

现在唯一的问题是 mp_limb_t 的大小。它可以是 32 位整数,也可以是 64 位整数,具体取决于平台。

  • 如果它是 32 位整数,那么您可以直接在 32 位整数数组上调用该函数。 (如果字节序匹配)
  • 如果它是 64 位整数,您仍然可以通过指针转换来使用它。 (取决于对齐方式和字节顺序)否则,您必须先将数组复制到 64 位整数数组中,然后才能调用 mpn_get_str

或者,使用 mpz 整数类可能更容易。 导入你的整数数组到一个大的整数,然后 打印 将其返回基地10。

Yes, you can use GMP for this. The function that you're looking for is mpn_get_str:

http://gmplib.org/manual/Low_002dlevel-Functions.html#Low_002dlevel-Functions

Now the only issue is the size of mp_limb_t. It is either a 32-bit integer or a 64-bit integer depending on the platform.

  • If it's a 32-bit integer, then you can call the function directly on your array of 32-bit integers. (if the endian matches)
  • If it's a 64-bit integer, you may be able to still use it with just a pointer cast. (depending on the alignment and the endianness) Otherwise, you'll have to copy your array into an array of 64-bit integers before you can call mpn_get_str.

Alternatively, it might be easier to use the mpz integer class. Import your integer array into a large integer, then print it back out in base 10.

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