The question
"How can I convert a string to a hex value?"
is often asked, but it's not quite the right question. Better would be
"How can I convert a hex string to an integer value?"
The reason is, an integer (or char or long) value is stored in binary fashion in the computer.
"6A" = 01101010
It is only in human representation (in a character string) that a value is expressed in one notation or another
"01101010b" binary
"0x6A" hexadecimal
"106" decimal
"'j'" character
all represent the same value in different ways.
But in answer to the question, how to convert a hex string to an int
char hex[] = "6A"; // here is the hex string
int num = (int)strtol(hex, NULL, 16); // number base 16
printf("%c\n", num); // print it as a char
printf("%d\n", num); // print it as decimal
printf("%X\n", num); // print it back as hex
Output:
j
106
6A
Answer from Weather Vane on Stack OverflowHow to convert string to hex value in C - Stack Overflow
String to HEX
How to convert string to hex in Python? - Stack Overflow
python - How to convert a hex string to hex number - Stack Overflow
The question
"How can I convert a string to a hex value?"
is often asked, but it's not quite the right question. Better would be
"How can I convert a hex string to an integer value?"
The reason is, an integer (or char or long) value is stored in binary fashion in the computer.
"6A" = 01101010
It is only in human representation (in a character string) that a value is expressed in one notation or another
"01101010b" binary
"0x6A" hexadecimal
"106" decimal
"'j'" character
all represent the same value in different ways.
But in answer to the question, how to convert a hex string to an int
char hex[] = "6A"; // here is the hex string
int num = (int)strtol(hex, NULL, 16); // number base 16
printf("%c\n", num); // print it as a char
printf("%d\n", num); // print it as decimal
printf("%X\n", num); // print it back as hex
Output:
j
106
6A
For converting string to hex value, and storing to variable:
char text[20] = "hello friend";
int text_len = strlen(text);
char *hex_output = malloc(text_len*2 + 1); // 2 hex = 1 ASCII char + 1 for null char
if (hex_output == NULL)
{
perror("Failed to allocated memory.");
return 1;
}
int hex_index = 0;
for (int i = 0; i < strlen(text); i++)
{
char t = text[i];
int w = sprintf(&hex_output[hex_index], "%x", t);
if (w < 0)
{
perror("Failed to convert to hex.");
return 1;
}
hex_index += 2;
}
hex_output[strlen(hex_output)] = 0x0;
printf("%s\n", hex_output);
Output:
68656c6c6f20667269656e64
I am wanting to take a normal string, "My water bottle is blue" for example, and converting that into the HEX representation of how it would be stored in a txt file. I've tried .encode, binascii.hexlify, and a few other things. Any help would be appreciated.
Try this:
hex_str = "0xAD4"
hex_int = int(hex_str, 16)
new_int = hex_int + 0x200
print hex(new_int)
If you don't like the 0x in the beginning, replace the last line with
print hex(new_int)[2:]
Use int function with second parameter 16, to convert a hex string to an integer. Finally, use hex function to convert it back to a hexadecimal number.
print hex(int("0xAD4", 16) + int("0x200", 16)) # 0xcd4
Instead you could directly do
print hex(int("0xAD4", 16) + 0x200) # 0xcd4