2016-07-24 218 views
0

我對大數使用mpz_t。我需要將mpz_t轉換爲二進制表示。我試圖使用mpz_export,但返回的數組只包含0。將mpz_t轉換爲二進制表示

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); 

回答

2

使用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

+1

https://gmplib.org/manual/Converting-Integers.html' mpz_get_str(char *,int,mpz_t)' – iksemyonov

+0

儘管OP看起來像是願意轉換成一系列連接在一起代表原始數字的字節序列,而不是一串'char'。 – iksemyonov