Friday, December 30, 2016

Convert String to Binary in Python

Code to convert string to binary and binary to string:

def string2bits(s=''):
    return [bin(ord(x))[2:].zfill(8) for x in s]

def bits2string(b=None):
    return ''.join([chr(int(x, 2)) for x in b])

si = input('\nenter text:\n')
b = string2bits(si)
so = bits2string(b)

print('\nyou entered\n' + si)

print('\nbinary is\n' + str(b))

print('\nstring is\n' + so)


  1. Organizations over each industry are presently actualizing AI applications to mechanize forms. machine learning course

  2. Hello I am so delighted I located your blog, I really located you by mistake, while I was watching on google for something else, Anyways I am here now and could just like to say thank for a tremendous post and a all round entertaining website. Please do keep up the great work. hoverwatch review