On Fri, Apr 21, 2023 at 7:39 PM Dwight Kulkarni <[email protected]> wrote:
>
> I need to convert Integer to unsigned char array or to vector<unsigned char>
>
> If I do this:
> for(int i=0; i<c.ByteCount(); i++){
> cout << int(c.GetByte(i)) << " ";
> }
>
> The values are not what I expect. Also, is there anything faster than a for
> loop to get the data out ?
The Integer class uses Encode when it needs to format an Integer as a
byte array for various functions, like DER encoding. You might try
something like:
#include <iostream>
#include "integer.h"
#include "osrng.h"
int main(int argc, char* argv[])
{
using namespace CryptoPP;
AutoSeededRandomPool prng;
Integer n;
n.Randomize(prng, 128);
const size_t len = n.MinEncodedSize(Integer::UNSIGNED);
std::vector<byte> v;
v.resize(len);
n.Encode((byte*)&v[0], v.size(), Integer::UNSIGNED);
std::cout << "Iostream: " << std::hex << n << std::endl;
std::cout << " Vector: ";
for(size_t i : v) { std::cout << (i & 0xff); }
std::cout << std::endl;
return 0;
}
It will produce output similar to:
jwalton@coffee:~/cryptopp$ g++ -g2 -O3 test.cxx ./libcryptopp.a -o test.exe
jwalton@coffee:~/cryptopp$ ./test.exe
Iostream: b32bd756bde62ea5124552714147af6eh
Vector: b32bd756bde62ea5124552714147af6e
jwalton@coffee:~/cryptopp$ ./test.exe
Iostream: fcbf89617f7f1cf55c1016b2355152e9h
Vector: fcbf89617f7f1cf55c1016b2355152e9
I'll get the example added to the wiki at
https://www.cryptopp.com/wiki/Integer .
Jeff
--
You received this message because you are subscribed to the Google Groups
"Crypto++ Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/cryptopp-users/CAH8yC8kooF2Z%3DMmt5krrdcFy50aewfJSt1y8W9MpxCaZNM1epw%40mail.gmail.com.