What is the best way to convert a variable length hex string e.g. \"01A1\"
to a byte array containing that data.
i.e converting this:
st
This implementation uses the built-in strtol
function to handle the actual conversion from text to bytes, but will work for any even-length hex string.
std::vector HexToBytes(const std::string& hex) {
std::vector bytes;
for (unsigned int i = 0; i < hex.length(); i += 2) {
std::string byteString = hex.substr(i, 2);
char byte = (char) strtol(byteString.c_str(), NULL, 16);
bytes.push_back(byte);
}
return bytes;
}