1

I want to transform the cypher in its base 256 of length xLen cypher (cypher is an instance of mpz_class). In order to do this I use the code below:

 //Write the integer x in its unique xLen-digit representation in base 256
 string str;
 str=cypher.get_str(256);

 //Print string figure by figure separated by space
for(int i=0;i<(int)str.length();i++){
    if(i%256==0)
        cout<<" "<<str[i];
    else
        cout<<str[i];
}

Unfortunately, I receive

terminate called after throwing an instance of 'std::logic_error' what(): basic_string::_M_construct null not valid Aborted (core dumped)

I strongly believe it's because of str=cypher.get_str(256) because changing the base to 10 returns no error.

I would appreciate a lot your ideas how I could replace the block. Thank you!

President James K. Polk
  • 40,516
  • 21
  • 95
  • 125
  • Ummm... "base256"? What does "base256" mean to you? – Sam Varshavchik Aug 25 '19 at 19:26
  • This is now the third time you've asked essentially this same question or a variant of it. `get_str()` returns an "ASCII" string (sequence of ASCII chars) in bases from 2 to 62. As mentioned in your previous question, you probably want `mpz_import` and `mpz_export`. – President James K. Polk Aug 25 '19 at 19:26
  • 2
    Yes It does look like OP is refining his question but that should be done with edits rather than reposting the question. – sgillen Aug 25 '19 at 19:29
  • I use now " void *r=NULL; size_t *countp = (size_t*) malloc(sizeof(size_t)); mpz_export (r, countp, 1, sizeof(char), 1, 0, cypher.get_mpz_t());" but r stays 0. Why happens this? –  Aug 25 '19 at 21:33
  • It probably only writes the value to a pre-allocated buffer instead of allocating it itself. This is still C/C++ where you have to perform a lot of the allocation / deallocation yourself. – Maarten Bodewes Aug 25 '19 at 23:12

1 Answers1

2

There is no such thing as an ASCII representation in base 256. Base 256 means that each digit contains 256 values. As a digit is stored as a character, each character must have at least 256 printable values. As ASCII only contains 95 printable values (i.e. no control characters such as backspace or the bell) any value above that will certainly not be accepted. You could do this with e.g. Unicode, but you would be missing the point of base encoding.

If somebody writes base 256 it probably simply means that it should be stored, enocoded or referenced in bytes, as each byte has 256 values. However, you'd still have to decide if the integers should be stored in a (two-complement) signed encoding or unsigned. Furthermore, a decision has to be made if it needs to be stored in big endian (network order, with the highest bit/ byte on the left) or little endian order as used on x86 compatible CPU's.


Unfortunately, the page for the mpz_export function is exceedingly unclear. Of course, the big decimals are used internally as 32 bit or - nowadays more likely - 64 bit words. However, this fact is exposed through this function and it seems only be able to write the values in a multiple of the words size, so 4 or 8 bytes, setting the unnecessary bytes to 0 values. This is an unnecessary nuisance.

Nevertheless, as mentioned in the comments, this function is what you should be using to convert the number to binary representation.

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
  • You are completely right about GMP. Unfortunately, that is not the only part of the documentation. Anyway, this library is a legend. – kelalaka Aug 29 '19 at 17:10