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 3 3115
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(con vbin[c] for c in s)
print binary("testing string")
Surely there are ways to make it shorter (But it's fast enough).
Bye,
bearophile
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
000010100101100 101100101011100 110010110000100 000011101110110 010101101100011 011000010110000 100000011011110 110011000100000 011000110110111 101110101011100 100111001101100 101001000000111 010001101000011 000010111010000 100111011100110 010000001101010 011101010111001 101110100001000 000111010001101 000011001010010 000001110011011 011110111001001 110100001000000 110111101100110 000010100110001 001101100011010 010110111001101 011011001010111 001001100101011 001000010000001 110000011010000 110100101101100 011010010111001 101110100011010 010110111001100 101001000000111 000001101001011 001110010000001 101001011001110 110111001101111 011100100110000 101101110011000 110110010100100 000010010010010 011101110110011 001010010000001 100011011011110 110110101100101 000010100111010 001101111001000 000110010101111 000011100000110 010101100011011 101000010000001 100110011100100 110111101101101 001000000111100 101101111011101 010010000001101 110011011110110 111000101101011 000110111001001 100101011000010 111010001101001 011101100110010 100100000011001 110110000101110 010011000100110 000101100111011 001010010111000 001010
Regards
Bussiere
Le dimanche 20 août 2006 13:55, be************@ lycos.com 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(con vbin[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')
011101000110010 101110011011101 000010000001110 011011101000111 001001101001011 0111001100111
Bye,
bearophile
--
_____________
Maric Michaud
_____________
Aristote - www.aristote.info
3 place des tapis
69004 Lyon
Tel: +33 426 880 097 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Mel WEaver |
last post by:
Hello,
I have the following delphi structure for a binary file. I'm looking
for idea how to read this file.
Mel
type
TMenuDataStruct = packed record
exename : string;
|
by: dale zhang |
last post by:
Hi,
I am trying to read an image from MS Access DB based on the following article:
http://www.vbdotnetheaven.com/Code/Sept2003/2175.asp
The article author is using PictureBox for windows application, while I am
doing for web. I can only find Image from web forms control and HTML control.
This may be the root cause of my problem. For...
|
by: dale zhang |
last post by:
Hi,
I am trying to save and read an image from MS Access DB based on the
following article:
http://www.vbdotnetheaven.com/Code/Sept2003/2175.asp
Right now, I saved images without any errors. After reading the ole object
from db, I saved it to C: as file1.bmp and displayed on the web. But it can
not be displayed. After I manually sent...
|
by: MrKrich |
last post by:
I want to convert Hexadecimal or normal integer to Binary. Does VB.Net has function to do that? I only found Hex function that convert normal integer to Hexadecimal.
|
by: yxq |
last post by:
There are 8 bytes binary value stored date and time in Registry.
84 8B D7 DF 8B 28 C5 01
I want to convert the binary value to date using VB.NET.
Dim a As FILETIME
a.dwHighDateTime = 29698187 '(01C5288B to decimal)
a.dwLowDateTime = -539522172 '(??????? by 84 8B D7 DF)
MsgBox (GetFileToSystemDate(a, False))
| |
by: elliotng.ee |
last post by:
I have a text file that contains a header 32-bit binary. For example,
the text file could be:
%%This is the input text
%%test.txt
Date: Tue Dec 26 14:03:35 2006
00000000000000001111111111111111
11111111111111111111111111111111
00000000000000000000000000000000
11111111111111110000000000000000
|
by: te509 |
last post by:
Hi guys,
does anybody know how to convert a long
sequence of bits to a bit-string? I want to avoid this:
'949456129574336313917039111707606368434510426593532217946399871489'
I would appreciate a prompt reply because I have a python assessment to
submit.
Thanks,
Thomas
|
by: Bob Altman |
last post by:
Hi all,
I'm looking for the fastest way to convert an array of bytes to String. I
also need to convert a String back to its original Byte() representation.
Convert.ToBase64String and Convert.FromBase64String seem like the closest
thing I can find to what I'm looking for baked into the base class library.
Can anyone suggest a better way...
|
by: xiao |
last post by:
Can I fullfill this task? Using fred and fwrite?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |