[Tutor] Concatenating string

Adam Bark adam.jtm30 at gmail.com
Tue Feb 22 15:07:52 CET 2011


On 22/02/11 13:46, tee chwee liong wrote:
> hi,
>
> >>> bin(0xff0)
> '111111110000'
> >>> bin(0xff1)
> '111111110001'
> >>> a=bin(0xff0)+bin(0xff1)
> >>> a
> '111111110000111111110001'
> >>> b=0xff0
> >>> c=0xff1
> >>> d=b+c
> >>> d
> 8161
> >>> bin(d)
> '1111111100001'
>
> question:
> 1) why is it that a and d values are different? i'm using Python 2.5.
> 2) how to convert hex to bin in Python 2.5?
>
>
> thanks
> tcl
Hi,

1) As you can see bin() returns a binary representation of the number 
you pass to it as a string. Thus when you do a=bin(0xff0)+bin(0xff1) you 
are concatenating the two strings returned by bin. For d you are 
carrying out the mathematical addition operation on the two numbers 
before converting it to binary.
2) You've already done that several times, just use bin()

HTH,
Adam.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110222/557ae98c/attachment.html>


More information about the Tutor mailing list