On 22/02/2011 14.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?

As Adam and Bob already pointed out, you are using two different conversion 
tools and obtain two different types.
If you need a binary representation of a number AS A STRING, you can use bin(n) as you did. Or you can use hex(n) to see an hexadecimal view of the same number, again AS A STRING. If you want a binary or hex NUMBER, you already have it: EVERY number is stored internally as a binary value, even if you enter or see it in decimal or in a different base. Look at the following transcript from the Python interpreter, and please pay attention to the presence or absence of the quotes:
.........................................
.       >>> hex(0b1001001001001001001)
.       '0x49249.'
.       >>> 0b1001001001001001001
.       299593
.       >>> 0x49249
.       299593
.       >>> bin(0x49249)
.       '0b1001001001001001001'
.       >>>
........................................

Hope this will help you.

Francesco Loffredo


-----
Nessun virus nel messaggio.
Controllato da AVG - www.avg.com
Versione: 10.0.1204 / Database dei virus: 1435/3457 -  Data di rilascio: 
21/02/2011

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to