Convert String to Hex in Python

The hex() function can be used to convert a string to a hexadecimal value. Before using the function the value has to be converted to bytes.

The value is returned as a string representing a hexadecimal value.

68656c6c6f

Split these values into hex numbers:

0x68 0x65 0x6c 0x6c 0x6f

You can see, how are these values calculated.

HexDecOperationASCII
0x681046*16^1 + 8*16^0 = 6*16 + 8 * 1 = 104h
0x651016*16^1 + 5*16^0 = 6*16 + 5 * 1 = 101e
0x6c1086*16^1 + 12*16^0 = 6*16 + 12 * 1 = 108l
0x6c1086*16^1 + 12*16^0 = 6*16 + 12 * 1 = 108l
0x6f1116*16^1 + 15*16^0 = 6*16 + 15 * 1 = 111o

Decode the value back to a string

If you want to decode this hex string into a string, you can use the following code, or read this tutorial.

This is the result:

hello