How to Convert ASCII to Binary in Python

In this lesson i want to show you How to Convert ASCII to Binary in Python.


What is ASCII ?

ASCII (American Standard Code for Information Interchange) is character encoding standard used to represent text in computers and other devices. it defines set of 128 characters, including letters, numbers, punctuation marks and other symbols, each of which is assigned a unique number between 0 and 127.

ASCII was first introduced in 1963 and is widely used in many countries. it was originally designed for use with teletypes and other early communications devices and its simplicity and ease of use made it popular choice for early computer systems. while ASCII is now largely obsolete in terms of modern character encoding standards but it remains an important part of computing history and is still used in many legacy systems.



What is Binary ?

Binary is number system that uses only two symbols, 0 and 1, to represent all of its values. unlike decimal (base-10) number system that we use in everyday life, binary is base-2 system, meaning that each place value can only hold the value of 0 or 1.

Binary is the foundation of modern computing as all data and information processed by computers is represented using binary code. in binary each character, letter or symbol is represented using unique combination of 0s and 1s known as bits. for example the number 8 in decimal is represented as 1000 in binary.

Binary is also used in digital electronics and other fields where information is processed in  binary format. the advantage of binary is its simplicity, as only two symbols are used, and its ease of processing, as binary can be represented by the on and off states of a simple switch.



How to Convert ASCII to Binary in Python ?

In Python you can convert an ASCII character to its binary representation using bin function and ord function. ord function returns the integer representation of a given ASCII character, and bin function returns a string representation of a binary number. this is an example:





In this example ascii_to_binary function takes an ASCII character as an input and returns its binary representation as a string. the slice [2:] is used to remove the 0b prefix from the binary representation returned by bin.


Learn More on Python

Leave a Comment