c++ - 将 mpz_t 转换为二进制表示

标签 c++ gmp

我使用 mpz_t 来表示大数字。我需要将 mpz_t 转换为二进制表示形式。我尝试使用 mpz_export,但返回的数组仅包含 0。

mpz_t test;
mpz_init(test);
string myString = "173065661579367924163593258659639227443747684437943794002725938880375168921999825584315046";
    mpz_set_str(test,myString.c_str(),10);
    int size = mpz_sizeinbase(test,2);
    cout << "size is : "<< size<<endl;
    byte *rop = new byte[size];
    mpz_export(rop,NULL,1,sizeof(rop),1,0,test);

最佳答案

使用 gmpxx(因为它被标记为 c++)

#include <iostream>
#include <gmpxx.h>

int main()
{
    mpz_class a("123456789");
    std::cout << a.get_str(2) << std::endl; //base 2 representation
}

普通 GMP 中应该有等效的功能

关于c++ - 将 mpz_t 转换为二进制表示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38551496/

相关文章:

c++ - struct 中的 std::vector 定义

c++ - 难以定位 for 循环中的问题

c++ - 如何识别大型 C++ 非托管应用程序中的错误转换?

C++ 不在任务栏中显示使用 CreateWindow 创建的窗口

c - 有什么方法可以在 msp430 中进行多精度运算(使用大于 64 位的整数)?

c - C 中的段错误(核心转储)(使用 GMP 库)

c++ - GMP:得到差异的绝对值?

java - 是什么导致了 GMP 的 JNA 包装器中的段错误

c++ - MPI calloc 导致段错误

c++ - 无法在 Windows 下以正常的方式使用 gmp 和 uint64_t