Sometimes, we need to view the content of a string in Python in the form of hexadecimal values, we can use the following function to convert each character to ASCII code and then hex value.
def hexify(s):
return [hex(ord(i)) for i in list(s)]
Implemented in One Line Lambda Function:
hexify = lambda s: [hex(ord(i)) for i in list(s)]
Example usage by converting string “abc” to Hex.
print(hexify("abcde"))
# ['0x61', '0x62', '0x63', '0x64', '0x65']
This is useful when the content/data/buffer contains non-printable ASCII code. To print the string in Hex using C function: The C Function to Print a Char Array (String) in Hexadecimal
–EOF (The Ultimate Computing & Technology Blog) —
Last Post: Teaching Kids Programming - Equal Tree Partition via Recursive Depth First Search Algorithm
Next Post: Teaching Kids Programming - Three Graph Algorithms: Does Every Vertex Have at least One Edge in Graph?