如何使用字符串中的 1024 位数字初始化 gmp 中的 mpz_t?

发布于 2024-11-19 16:07:37 字数 79 浏览 6 评论 0原文

我想用一个非常大的值(例如 1024 位大整数)初始化 gmp 中的 mpz_t 变量。我怎样才能这样做呢?我是gmp新手。任何帮助将不胜感激。

I want to initialize a mpz_t variable in gmp with a very large value like a 1024 bit large integer. How can I do so ? I am new to gmp. Any help would be appreciated.

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

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

发布评论

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

评论(2

短暂陪伴 2024-11-26 16:07:37

使用mpz_import。例如:

uint8_t input[128];
mpz_t z;
mpz_init(z);

// Convert the 1024-bit number 'input' into an mpz_t, with the most significant byte
// first and using native endianness within each byte.
mpz_import(z, sizeof(input), 1, sizeof(input[0]), 0, 0, input);

Use mpz_import. For example:

uint8_t input[128];
mpz_t z;
mpz_init(z);

// Convert the 1024-bit number 'input' into an mpz_t, with the most significant byte
// first and using native endianness within each byte.
mpz_import(z, sizeof(input), 1, sizeof(input[0]), 0, 0, input);
多像笑话 2024-11-26 16:07:37

要从 C++ 中的字符串初始化 GMP 整数,您可以使用 libgmp++ 并直接使用构造函数:

#include <gmpxx.h>

const std::string my_number = "12345678901234567890";

mpz_class n(my_number); // done!

如果您仍然需要原始 mpz_t 类型,请说 n。 get_mpz_t()。

在 C 语言中,您必须这样拼写:

#include <gmp.h>

const char * const my_number = "12345678901234567890";
int err;

mpz_t n;
mpz_init(n);
err = mpz_set_str(n, my_number);    /* check that err == 0 ! */

/* ... */

mpz_clear(n);

请参阅文档了解初始化整数的更多方法。

To initialize a GMP integer from a string in C++, you can use libgmp++ and directly use a constructor:

#include <gmpxx.h>

const std::string my_number = "12345678901234567890";

mpz_class n(my_number); // done!

If you still need the raw mpz_t type, say n.get_mpz_t().

In C, you have to spell it out like this:

#include <gmp.h>

const char * const my_number = "12345678901234567890";
int err;

mpz_t n;
mpz_init(n);
err = mpz_set_str(n, my_number);    /* check that err == 0 ! */

/* ... */

mpz_clear(n);

See the documentation for further ways to initialize integers.

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