[python]ASCII to integer and vice versa

There are two functions in python which can convert the ascii to the integer equivalent and vice versa.
To convert ascii character to integer representation use ord(), to convert integer to ascii representation use chr().

Example:

plaintext = "hello world!"
ascii_to_num = [ord(pt) for pt in plaintext]
print(ascii_to_num)
num_to_ascii = ''.join(chr(an) for an in ascii_to_num)
print(num_to_ascii)

Result

[104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 33]
hello world!

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s