How to Convert a string into binary
Terry Reedy
tjreedy at udel.edu
Sat Apr 15 18:09:25 EDT 2006
"HNT20" <hnt20 at msn.com> wrote in message
news:KMb0g.92182$9I5.52507 at tornado.ohiordc.rr.com...
> i am new to python language. i am working on a gnuradio project where it
> uses python as the primary programming language. i am trying to convert
> a message, text, or numbers into binary code so that i can process it.
....
> hi --> 0110100001101001
I would start by making a lookup table to translate the ordinals of ascii
chars to binary strings:
a2b = ['00000000', '00000001', '00000010', etc ...
using something that generates the binary strings (such as that posted).
If you don't want any or all of the initial 32 control chars translated,
replace the corresponding string with ''.
Then your main program is very simple:
# given string s
binchars = []
for c in s: binchars.append(a2b[ord(c)])
#if you really then need one long string instead of the list of strings
longstring = ''.join(binchars)
#or use any other connector than '', such as ' '.
Terry Jan Reedy
More information about the Python-list
mailing list