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 sb = '0'*(8-len(sb)) + sb #add leading 0's ss += sb print ss 00001010010110010110010101110011001011000010000001110111011001010110110001101100001011000010000001101111011001100010000001100011011011110111010101110010011100110110010100100000011101000110100001100001011101000010011101110011001000000110101001110101011100110111010000100000011101000110100001100101001000000111001101101111011100100111010000100000011011110110011000001010011000100110110001101001011011100110101101100101011100100110010101100100001000000111000001101000011010010110110001101001011100110111010001101001011011100110010100100000011100000110100101100111001000000110100101100111011011100110111101110010011000010110111001100011011001010010000001001001001001110111011001100101001000000110001101101111011011010110010100001010011101000110111100100000011001010111100001110000011001010110001101110100001000000110011001110010011011110110110100100000011110010110111101110101001000000110111001101111011011100010110101100011011100100110010101100001011101000110100101110110011001010010000001100! 1110110000101110010011000100110000101100111011001010010111000001010 > Regards > Bussiere -- http://mail.python.org/mailman/listinfo/python-list