I'm working on a very old machine in which the SDK I use to compile my C++ executable's does not support string, so I need to work with char arrays.
This code works fine for converting a string to hex
std::string string_to_hex(const std::string& input)
{
static const char* const lut = "0123456789ABCDEF";
size_t len = input.length();
std::string output;
output.reserve(2 * len);
for (size_t i = 0; i < len; ++i)
{
const unsigned char c = input[i];
output.push_back(lut[c >> 4]);
output.push_back(lut[c & 15]);
}
return output;
}
But the function works with the string data type, which I cannot use.
I've tried using this as well, but to no avail.
char *hextostrTest(char *hexStr)
{
size_t len = strlen(hexStr);
int k = 0;
if (len & 1) return NULL;
char* output = new char[(len / 2) + 1];
for (size_t i = 0; i < len; i += 2)
{
output[k++] = (((hexStr[i] >= 'A') ? (hexStr[i] - 'A' + 10) : (hexStr[i] - '0')) << 4) |
(((hexStr[i + 1] >= 'A') ? (hexStr[i + 1] - 'A' + 10) : (hexStr[i + 1] - '0')));
}
output[k] = '\0';
return output;
}
stringpart but rewrite the whole function?reserve()andpush_back()functions to work with char arrays would be a pain in the ass.reserve()with alloc memory,push_backis pretty easy to implement. Anyway, if you havehextostrwithstd::string, post it.string_to_hexhelps nothing