Python Text Processing Useful Resources

Python Text Processing - Binary ASCII Conversion



The ASCII to binary and binary to ascii conversion is carried out by the in-built binascii module. It has a very straight forward usage with functions which take the input data and do the conversion. The below program shows the use of binascii module and its functions named b2a_uu and a2b_uu. The uu stands for "UNIX-to-UNIX encoding" which takes care of the data conversion from strings to binary and ascii values as required by the program.

Binary ASCII Conversion

main.py

import binascii

text = b"Simply Easy Learning"

# Converting binary to ascii
data_b2a = binascii.b2a_uu(text)
print("**Binary to Ascii** \n")
print(data_b2a)

# Converting back from ascii to binary 
data_a2b = binascii.a2b_uu(data_b2a)
print("**Ascii to Binary** \n")
print(data_a2b)

Output

When we run the above program we get the following output −

**Binary to Ascii** 

b'44VEM<&QY($5A<WD@3&5A<FYI;F< \n'
**Ascii to Binary** 

b'Simply Easy Learning'
Advertisements