# convert a long string in binary

Discussion in 'Python' started by bussiere maillist, Aug 20, 2006.

1. ### bussiere maillistGuest

i've got a very long string
and i wanted to convert it in binary
like

string = """Monty Python, or The Pythons, is the collective name of
the creators of Monty Python's Flying Circus, a British television
comedy sketch show that first aired on the BBC on October 5, 1969. A
total of 45 episodes were made over four series. However, the Python
phenomenon was much greater, spawning stage tours, a musical, four
films, numerous albums, and several books, as well as launching the
members to individual stardom.

The television series, broadcast by the BBC from 1969 to 1974, was
conceived, written and performed by Graham Chapman, John Cleese
(1969-1973), Terry Gilliam, Eric Idle, Terry Jones and Michael Palin.
Loosely structured as a sketch show, but with a highly innovative
stream-of-consciousness approach (aided by Terry Gilliam's
animations), it pushed the boundaries of what was then considered
acceptable, both in terms of style and content.
"""

string = binary(string)
print string
// 01010101010101

i 'am searching for something like the binary function (taht doesn't
exist) to convert my string directly in binary.

Regards
Bussiere

bussiere maillist, Aug 20, 2006

2. ### Guest

bussiere maillist:
> i've got a very long string
> and i wanted to convert it in binary

Not much tested:

_nibbles = {"0":"0000", "1":"0001", "2":"0010", "3":"0011",
"4":"0100", "5":"0101", "6":"0110", "7":"0111",
"8":"1000", "9":"1001", "A":"1010", "B":"1011",
"C":"1100", "D":"1101", "E":"1110", "F":"1111"}

def toBase2(number):
if number < 16:
return "0000" + _nibbles["%X" % number]
else:
d1, d2 = "%X" % number
return _nibbles[d1] + _nibbles[d2]

convbin = dict((chr(i), toBase2(i)) for i in xrange(256))

def binary(s):
return "".join(convbin[c] for c in s)

print binary("testing string")

Surely there are ways to make it shorter (But it's fast enough).

Bye,
bearophile

, Aug 20, 2006

3. ### Guest

bussiere maillist wrote:
> i've got a very long string
> and i wanted to convert it in binary
> like
>
> string = """Monty Python, or The Pythons, is the collective name of
> the creators of Monty Python's Flying Circus, a British television
> comedy sketch show that first aired on the BBC on October 5, 1969. A
> total of 45 episodes were made over four series. However, the Python
> phenomenon was much greater, spawning stage tours, a musical, four
> films, numerous albums, and several books, as well as launching the
> members to individual stardom.
>
> The television series, broadcast by the BBC from 1969 to 1974, was
> conceived, written and performed by Graham Chapman, John Cleese
> (1969-1973), Terry Gilliam, Eric Idle, Terry Jones and Michael Palin.
> Loosely structured as a sketch show, but with a highly innovative
> stream-of-consciousness approach (aided by Terry Gilliam's
> animations), it pushed the boundaries of what was then considered
> acceptable, both in terms of style and content.
> """
>
> string = binary(string)
> print string
> // 01010101010101
>
>
> i 'am searching for something like the binary function (taht doesn't
> exist) to convert my string directly in binary.

The gmpy module can convert numbers to binary strings:

import gmpy

s = """
Yes, well, of course that's just the sort of
blinkered philistine pig ignorance I've come
to expect from you non-creative garbage.
"""

ss = ''
for c in s:
sb = gmpy.digits(ord(c),2) #convert to binary string
ss += sb

print ss

000010100101100101100101011100110010110000100000011101110110010101101100011011000010110000100000011011110110011000100000011000110110111101110101011100100111001101100101001000000111010001101000011000010111010000100111011100110010000001101010011101010111001101110100001000000111010001101000011001010010000001110011011011110111001001110100001000000110111101100110000010100110001001101100011010010110111001101011011001010111001001100101011001000010000001110000011010000110100101101100011010010111001101110100011010010110111001100101001000000111000001101001011001110010000001101001011001110110111001101111011100100110000101101110011000110110010100100000010010010010011101110110011001010010000001100011011011110110110101100101000010100111010001101111001000000110010101111000011100000110010101100011011101000010000001100110011100100110111101101101001000000111100101101111011101010010000001101110011011110110111000101101011000110111001001100101011000010111010001101001011101100110010100100000011001110110000101110010011000100110000101100111011001010010111000001010

> Regards
> Bussiere

, Aug 20, 2006
4. ### Maric MichaudGuest

Le dimanche 20 août 2006 13:55, a écrit :
> bussiere maillist:
> > i've got a very long string
> > and i wanted to convert it in binary

>
> Not much tested:
>
> _nibbles = {"0":"0000", "1":"0001", "2":"0010", "3":"0011",
> "4":"0100", "5":"0101", "6":"0110", "7":"0111",
> "8":"1000", "9":"1001", "A":"1010", "B":"1011",
> "C":"1100", "D":"1101", "E":"1110", "F":"1111"}
>
> def toBase2(number):
> if number < 16:
> return "0000" + _nibbles["%X" % number]
> else:
> d1, d2 = "%X" % number
> return _nibbles[d1] + _nibbles[d2]
>
> convbin = dict((chr(i), toBase2(i)) for i in xrange(256))
>
> def binary(s):
> return "".join(convbin[c] for c in s)
>
> print binary("testing string")
>
> Surely there are ways to make it shorter (But it's fast enough).
>

Maybe this one is more elegant :

In [305]: trans = {}

In [306]: for i in range(256) :
.....: trans[chr(i)] = ''.join(i & 2**j and '1' or '0'
.....: for j in reversed(range(8)))
.....:

In [307]: trans['a']
Out[307]: '01100001'

In [308]: trans['\xee']
Out[308]: '11101110'

In [309]: print ''.join(trans[e] for e in 'test string')
0111010001100101011100110111010000100000011100110111010001110010011010010110111001100111

> Bye,
> bearophile

--
_____________

Maric Michaud
_____________

Aristote - www.aristote.info
3 place des tapis
69004 Lyon
Tel: +33 426 880 097

Maric Michaud, Aug 22, 2006