ASCII to Binary Conversion in Python

ASCII, which stands for the American Standard Code for Information Interchange, became a communication standard in 1967 and has since played a foundational role in character encoding for computers and electronic devices. Binary is a base-2 numeral system. In this guide, we delve into the Python methods for converting binary to ASCII and vice versa.

Binary to ASCII Conversion Using Python Code

Python makes it straightforward to convert binary data to ASCII. The int function is particularly useful for this purpose. Consider the following example:

binary_string = '01001000 01100101 01101100 01101100 01101111'
ascii_text = ''.join([chr(int(binary, 2)) for binary in binary_string.split(' ')])
print(ascii_text)

The code above may look a little complex. A good tip with these kind of compound statements is think about what happens within the parentheses (round or square) and work your way outwards.

Output:

Hello

Additional Example

Let’s convert another binary string to ASCII.

binary_string = '01000010 01100101 01110111 01100001 01110010 01100101 00100000 01110100 01101000 01100101 00100000 01000010 01101100 01110101 01100101 00100000 01001101 01100101 01100001 01101110 01101001 01100101 01110011 00101110'
ascii_text = ''.join([chr(int(binary, 2)) for binary in binary_string.split(' ')])
print(ascii_text)

Again, think about what happens within the parentheses and work your way outwards.

Output:

Beware the Blue Meanies.

ASCII to Binary Conversion Using Python Code

Converting ASCII to binary in Python is just as straightforward. The ord function returns the Unicode code point of a given character, and f-strings make the code more readable.

ascii_text = "Binary"
binary_string = ' '.join([f'{ord(char):08b}' for char in ascii_text])
print(binary_string)

Here we are making use of Python’s “f-strings” which provide a powerful set of tools for formatting output.

Output:

01000010 01101001 01101110 01100001 01110010 01111001

Additional Example

Convert the ASCII string ‘I’m a smart coder’ to binary.

ascii_text = "I'm a smart coder"
binary_string = ' '.join([f'{ord(char):08b}' for char in ascii_text])
print(binary_string)

Output:

01001001 00100111 01101101 00100000 01100001 00100000 01110011 01101101 01100001 01110010 01110100 01100011 01101111 01100100 01100101 01110010

Binary to ASCII Table

Here is sample of a table for converting from Binary to ASCII.

Binary ASCII
01000001 A
01000010 B
01000011 C
01000100 D
01000101 E
01000110 F
01000111 G
01001000 H
01001001 I
01001010 J

Note: The table above provides only a few examples for brevity. A full table can be found here.

Conclusion

Python offers powerful tools for converting data between binary and ASCII representations. Whether you’re dealing with binary-encoded messages or converting text to binary for storage, mastering these techniques is essential. The examples provided should serve as a solid foundation for your binary and ASCII conversion endeavours in Python. As you explore further, you’ll discover the versatility and efficiency that Python brings to these common programming tasks.

Sharing is caring!

Leave a Reply

Your email address will not be published. Required fields are marked *