1

In Java, there is a BigInteger class to using large numbers, and it has a converter function: toByteInteger like this:

private static final BigInteger N = new BigInteger(HEX_N, 16);
...
byte[] digest = messageDigest.digest(N.toByteArray());

In C++, I tried Boost.Multiprecision to use large number type with mpz_int, but it has not any function which convert to mpz_int to byte array.

Is there any equivalent BigInteger type in C++? I'm working with qt framework, is there any big integer structure on qt? And is it possible to convert it to byte array?

gkhanacer
  • 575
  • 7
  • 25
  • How about just casting reference of the value inside it to `char*` or `void*`? wouldn't be your byte array? – The Quantum Physicist Feb 23 '17 at 13:16
  • in the [docs](http://www.boost.org/doc/libs/1_55_0/libs/multiprecision/doc/html/boost_multiprecision/ref/number.html) I found `convert_to<>`. Did you try this? – 463035818_is_not_an_ai Feb 23 '17 at 13:17
  • or v.backend().data().mpz_export(...), see: http://www.boost.org/doc/libs/1_63_0/libs/multiprecision/doc/html/boost_multiprecision/tut/ints/gmp_int.html and https://gmplib.org/manual/Integer-Import-and-Export.html#Integer-Import-and-Export – Dragos Pop Feb 23 '17 at 13:49
  • @tobi303 Firstly, I convert mpz_int to std::string, than I used string's c_str() method. thank you. – gkhanacer Feb 23 '17 at 14:15

1 Answers1

0

Don´t know any specific function, but both cases below have worked to convert cpp_int to byte array!!!

unsigned char cSHA[33], *xzh;
cpp_int ichA;
ichA = g[0];

//CONVERSION USING BINARY MATHS

for (int i = 32; i>= 1; i--)
{
    // este cast pode dar problemas em UBUNTU
    cSHA[i] = (unsigned char)(ichA & 255);
    ichA = ichA/256;
}

// OR   
//CONVERSION USING POINTER
xzh = ((unsigned char*) &ichA) + 32;

for (int i = 32; i>= 1; i--)
{
    cSHA[i] = *(xzh - i);
}