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.
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to