[Tutor] Applying a mask
Kirby Urner
urnerk@qwest.net
Wed, 21 Nov 2001 08:05:04 -0800
At 04:13 PM 11/21/2001 +0100, you wrote:
>Dear all,
>
>I have a string, containing a 63-digit hexadecimal number.
>I want to build a function which extracts bits from that
>hexadecimal number, and show it as an integer (unsigned)
>value.
The long integer is a place to store big hex numbers.
In Python 2.2, we're starting to not need to specify
when an integer is long, with the 'L' suffix.
>>> myhex = eval('0x'+'A323332BFE23231')
>>> myhex
734705982275465777L
eval('0x'+yourstring) will need to be written
eval('0x'+yourstring+'L') if your Python is < 2.2.
Then you can AND this with another long.
>>> hex2 = eval('0x9234237942')
>>> result = myhex & hex2
>>> result
78184067072L
If you want to convert back to hex, go:
>>> hex(result)
'0x1234223000L'
hex returns a string, which you can eval into a
long again if need be.
Shifting left and right is done on the longs, using
the << and >> operators.
Kirby