binary literal

Dave Angel davea at ieee.org
Wed Jul 22 17:10:35 CEST 2009


superpollo wrote:
> <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
> E
> >>>
>
> can i do something like the above, but using a *binary* number? (e.g. 
> 00101101 instead of 45) ?
>
> bye
>
> </div>
>
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
print x
print chr(x)

output is:
  45
  -

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]
print lis
s= "".join(chr(x) for x in lis)
print s

DaveA




More information about the Python-list mailing list