How to Convert a string into binary
Duncan Booth
duncan.booth at invalid.invalid
Sat Apr 15 16:18:03 EDT 2006
HNT20 wrote:
> is there a way to convert a string into its binary representation of the
> ascii table.??
>
> an example of what i want would be:
>
> hi --> 0110100001101001
Why not just write some code to do it? e.g.
>>> def b1(n):
return "01"[n%2]
>>> def b2(n):
return b1(n>>1)+b1(n)
>>> def b3(n):
return b2(n>>2)+b2(n)
>>> def b4(n):
return b3(n>>4)+b3(n)
>>> bytes = [ b4(n) for n in range(256)]
>>> def binstring(s):
return ''.join(bytes[ord(c)] for c in s)
>>> binstring('hi')
'0110100001101001'
More information about the Python-list
mailing list