One-line Python Lambda Function to Hexify a String/Data (Converting ASCII code to Hexadecimal)


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.

1
2
def hexify(s):
    return [hex(ord(i)) for i in list(s)]   
def hexify(s):
    return [hex(ord(i)) for i in list(s)]   

Implemented in One Line Lambda Function:

1
hexify = lambda s: [hex(ord(i)) for i in list(s)]
hexify = lambda s: [hex(ord(i)) for i in list(s)]

Example usage by converting string “abc” to Hex.

1
2
print(hexify("abcde"))
# ['0x61', '0x62', '0x63', '0x64', '0x65']
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) —

GD Star Rating
loading...
213 words
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?

The Permanent URL is: One-line Python Lambda Function to Hexify a String/Data (Converting ASCII code to Hexadecimal)

Leave a Reply