I am working in C++ and let's say I have the following hexadecimal as a string
string key = "F3D5";
how can I convert it to an array of uint8_t
?
Actually, I have an algorithm that generate a key of type uint8_t*
, and I convert this key to hexadecimal using the following code :
uint8_t key = generateKey();
vector<uint8_t> keyVector(begin(key), end(key));
string hexadecimalKey= uint8_vector_to_hex_string(keyVector);
This is the method that convert uint8 vector to hexadecimal in string :
string uint8_vector_to_hex_string(const vector<uint8_t>& v) {
stringstream ss;
ss << hex << setfill('0');
vector<uint8_t>::const_iterator it;
for (it = v.begin(); it != v.end(); it++) {
ss << setw(2) << static_cast<unsigned>(*it);
}
return ss.str();
}
And I would like to re-convert the hexadecimalKey
to uint8_t, with the following code
uint8_t* convertedKey = hex_string_to_uint8_t(hexadecimalKey);
with the following method :
uint8_t* hex_string_to_uint8_t(string value) {
size_t len = value.length();
vector<uint8_t> out;
for (size_t i = 0; i < len; i += 2) {
std::istringstream strm(value.substr(i, 2));
uint8_t x;
strm >> std::hex >> x;
out.push_back(x);
}
return out.data();
}
and whenever i do that, the convertedKey
is not equal to key
I need your help.
Thank you, really appreciate it