Convert binary to ASCII and vice versa Convert binary to ASCII and vice versa python python

Convert binary to ASCII and vice versa


For ASCII characters in the range [ -~] on Python 2:

>>> import binascii>>> bin(int(binascii.hexlify('hello'), 16))'0b110100001100101011011000110110001101111'

In reverse:

>>> n = int('0b110100001100101011011000110110001101111', 2)>>> binascii.unhexlify('%x' % n)'hello'

In Python 3.2+:

>>> bin(int.from_bytes('hello'.encode(), 'big'))'0b110100001100101011011000110110001101111'

In reverse:

>>> n = int('0b110100001100101011011000110110001101111', 2)>>> n.to_bytes((n.bit_length() + 7) // 8, 'big').decode()'hello'

To support all Unicode characters in Python 3:

def text_to_bits(text, encoding='utf-8', errors='surrogatepass'):    bits = bin(int.from_bytes(text.encode(encoding, errors), 'big'))[2:]    return bits.zfill(8 * ((len(bits) + 7) // 8))def text_from_bits(bits, encoding='utf-8', errors='surrogatepass'):    n = int(bits, 2)    return n.to_bytes((n.bit_length() + 7) // 8, 'big').decode(encoding, errors) or '\0'

Here's single-source Python 2/3 compatible version:

import binasciidef text_to_bits(text, encoding='utf-8', errors='surrogatepass'):    bits = bin(int(binascii.hexlify(text.encode(encoding, errors)), 16))[2:]    return bits.zfill(8 * ((len(bits) + 7) // 8))def text_from_bits(bits, encoding='utf-8', errors='surrogatepass'):    n = int(bits, 2)    return int2bytes(n).decode(encoding, errors)def int2bytes(i):    hex_string = '%x' % i    n = len(hex_string)    return binascii.unhexlify(hex_string.zfill(n + (n & 1)))

Example

>>> text_to_bits('hello')'0110100001100101011011000110110001101111'>>> text_from_bits('110100001100101011011000110110001101111') == u'hello'True


Built-in only python

Here is a pure python method for simple strings, left here for posterity.

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])s = 'Hello, World!'b = string2bits(s)s2 = bits2string(b)print 'String:'print sprint '\nList of Bits:'for x in b:    print xprint '\nString:'print s2

String:Hello, World!List of Bits:01001000011001010110110001101100011011110010110000100000010101110110111101110010011011000110010000100001String:Hello, World!


I'm not sure how you think you can do it other than character-by-character -- it's inherently a character-by-character operation. There is certainly code out there to do this for you, but there is no "simpler" way than doing it character-by-character.

First, you need to strip the 0b prefix, and left-zero-pad the string so it's length is divisible by 8, to make dividing the bitstring up into characters easy:

bitstring = bitstring[2:]bitstring = -len(bitstring) % 8 * '0' + bitstring

Then you divide the string up into blocks of eight binary digits, convert them to ASCII characters, and join them back into a string:

string_blocks = (bitstring[i:i+8] for i in range(0, len(bitstring), 8))string = ''.join(chr(int(char, 2)) for char in string_blocks)

If you actually want to treat it as a number, you still have to account for the fact that the leftmost character will be at most seven digits long if you want to go left-to-right instead of right-to-left.