I am interested in taking in a single character.

c="c" # for example
hex_val_string = char_to_hex_string(c)
print hex_val_string



What is the simplest way of going about this? Any predefined string library stuff?

There are several ways of doing this:

>>> hex(ord("c"))
>>> format(ord("c"), "x")
>>> import codecs
>>> codecs.encode(b"c", "hex")

On Python 2, you can also use the hex encoding like this (doesn’t work on Python 3+):

>>> "c".encode("hex")

This might help

import binascii

x = b'test'
x = binascii.hexlify(x)
y = str(x,'ascii')

print(x) # Outputs b'74657374' (hex encoding of "test")
print(y) # Outputs 74657374

x_unhexed = binascii.unhexlify(x)
print(x_unhexed) # Outputs b'test'

x_ascii = str(x_unhexed,'ascii')
print(x_ascii) # Outputs test

This code contains examples for converting ASCII characters to and from hexadecimal. In your situation, the line you’d want to use is str(binascii.hexlify(c),'ascii').

Considering your input string is in the inputString variable, you could simply apply .encode('utf-8').hex() function on top of this variable to achieve the result.

inputString = "Hello"
outputString = inputString.encode('utf-8').hex()

The result from this will be 48656c6c6f.

You can do this:

your_letter = input()
def ascii2hex(source):
    return hex(ord(source))

For extra information, go to:

to get ascii code use ord(“a”);
to convert ascii to character use chr(97)