C++ Convert String to Hexadecimal and Vice Versa

How to convert a string to hex and vice versa in c?

sprintf and sscanf are good enough for this.

#include <stdio.h>
#include <string.h>

int main(void) {
char text[] = "thank you";
int len = strlen(text);

char hex[100], string[50];

// Convert text to hex.
for (int i = 0, j = 0; i < len; ++i, j += 2)
sprintf(hex + j, "%02x", text[i] & 0xff);

printf("'%s' in hex is %s.\n", text, hex);

// Convert the hex back to a string.
len = strlen(hex);
for (int i = 0, j = 0; j < len; ++i, j += 2) {
int val[1];
sscanf(hex + j, "%2x", val);
string[i] = val[0];
string[i + 1] = '\0';
}

printf("%s as a string is '%s'.\n", hex, string);

return 0;
}

Now

$ ./a.out
'thank you' in hex is 7468616e6b20796f75.
7468616e6b20796f75 as a string is 'thank you'.

C++ convert string to hexadecimal and vice versa

A string like "Hello World" to hex format: 48656C6C6F20576F726C64.

Ah, here you go:

#include <string>

std::string string_to_hex(const std::string& input)
{
static const char hex_digits[] = "0123456789ABCDEF";

std::string output;
output.reserve(input.length() * 2);
for (unsigned char c : input)
{
output.push_back(hex_digits[c >> 4]);
output.push_back(hex_digits[c & 15]);
}
return output;
}

#include <stdexcept>

int hex_value(unsigned char hex_digit)
{
static const signed char hex_values[256] = {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
};
int value = hex_values[hex_digit];
if (value == -1) throw std::invalid_argument("invalid hex digit");
return value;
}

std::string hex_to_string(const std::string& input)
{
const auto len = input.length();
if (len & 1) throw std::invalid_argument("odd length");

std::string output;
output.reserve(len / 2);
for (auto it = input.begin(); it != input.end(); )
{
int hi = hex_value(*it++);
int lo = hex_value(*it++);
output.push_back(hi << 4 | lo);
}
return output;
}

(This assumes that a char has 8 bits, so it's not very portable, but you can take it from here.)

Cant convert string hex into hexint and then into UIColor [duplicate]

You have to remove the 0x prefix and then specify the radix 16:

let s = "0xe7c79d"
print(Int(s)) // nil

let value = s.hasPrefix("0x")
? String(s.dropFirst(2))
: s
print(Int(value, radix: 16)) // 15189917


Related Topics



Leave a reply



Submit