davea at ieee.org
Wed Jul 22 17:10:35 CEST 2009
> <div class="moz-text-flowed" style="font-family: -moz-fixed">hello clp.
> i can insert a hex value for a character literal in a string:
> >>> stuff = "\x45"
> >>> print stuff
> can i do something like the above, but using a *binary* number? (e.g.
> 00101101 instead of 45) ?
There's no way to get a binary value directly into a literal, but you
can convert one into a string value, as follows:
x = 0b00101101
Note that this is decimal 45, which is different than the hex 45 you
used in your first example.
For doing multiple characters, I can't see anything better than:
lis = [0b00101101, 0b01000101, 0b01100001]
s= "".join(chr(x) for x in lis)
More information about the Python-list