Wireless Army
This is a blog / tips and tricks website for web developers and security researchers.
follow us in feedly


text to binary
by admin
 at 2017-09-06 00:35:00.

if you just want to convert normal text to binary you, this code will be use full. it will get a number for every letter using python's own library ex: ord('d') outputs 100 and then it will take the number and convert it to binary
#!/usr/bin/env python
 
BITS = ('0', '1')
ASCII_BITS = 7

def display_bits(b):
    """converts list of {0, 1}* to string"""
    return ''.join([BITS[e] for e in b])

def pad_bits(bits, pad):
    """pads seq with leading 0s up to length pad"""
    assert len(bits) <= pad
    return [0] * (pad - len(bits)) + bits
    
def convert_to_bits(n):
    """converts an integer `n` to bit array"""
    result = []
    if n == 0:
        return [0]
    while n > 0:
        result = [(n % 2)] + result
        n = n / 2
    return result

def string_to_bits(s):
    def chr_to_bit(c):
        return pad_bits(convert_to_bits(ord(c)), ASCII_BITS)
    return [b for group in 
            map(chr_to_bit, s)
            for b in group]

#it will get the user input as a string
stringtobit = raw_input("Enter something: ")
#it will print out the result
print display_bits(string_to_bits(stringtobit))