简体   繁体   中英

How to write the content of a void pointer?

How is it possible to cout bit-by-bit the bits stored at a void pointer in cpp?

void * r = malloc(k*8);
size_t count;
//Print string under byte form with bytes separated by space
cout << mpz_export (r, &count, 1, sizeof(char), 1, 0, cypher.get_mpz_t());

This cout writes only the address.

I think you are looking for this

Function: size_t mpz_out_str (FILE *stream, int base, const mpz_t op)

Output op on stdio stream stream, as a string of digits in base base. The base argument may vary from 2 to 62 or from -2 to -36.For base in the range 2..36, digits and lower-case letters are used; for -2..-36, digits and upper-case letters are used; for 37..62, digits, upper-case letters, and lower-case letters (in that significance order) are used. Return the number of bytes written, or if an error occurred, return 0

More information can be found here : https://gmplib.org/manual/I_002fO-of-Integers.html

Like this

char* ptr = (char*)mpz_export(r, &count, 1, sizeof(char), 1, 0, cypher.get_mpz_t());
for (int i = 0; i < count; ++i)
    cout << (unsigned)(unsigned char)ptr[i] << ' ';
cout << '\n';

Since you data is char data, you must cast the void pointer to a char pointer. Then to see the output as unsigned integers (which I presume is what you want) then you have to cast each char to an unsigned char and then an unsigned .

If you want hex output then just add the usual formatting code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM