I have string of hexadecimals which I need to convert to const byte*
...
But it will be in string. I need it to be in byte*
You should use a HexDecoder and ArraySink then. Something like:
string encoded = "FFEEDDCCBBAA99887766554433221100";
ASSERT(encoded.length() % 2 == 0);
size_t length = encoded.length() / 2;
unique_ptr<byte[]> decoded(new byte[length]);
StringSource ss(encoded, true /*pumpAll*/, new ArraySink(decoded.get(), length));
You can then use the byte array decoded.get()
as a byte*
.
You can also use a vector<byte>
. In this case, the byte*
is &v[0]
. Something like:
string encoded = "FFEEDDCCBBAA99887766554433221100";
ASSERT(encoded.length() % 2 == 0);
size_t length = encoded.length() / 2;
vector<byte> decoded;
decoded.resize(length);
StringSource ss(encoded, true /*pumpAll*/, new ArraySink(&decoded[0], length));
(comment) But it will be in string. I need it to be in byte*
This is even easier:
string encoded = "FFEEDDCCBBAA99887766554433221100";
string decoded;
StringSource ss(encoded, true /*pumpAll*/, new StringSink(decoded));
const byte* data = reinterpret_cast<const byte*>(decoded.data());
If you want the non-const version then use:
byte* ptr = reinterpret_cast<byte*>(&decoded[0]);