how to separate hexadecimal
Nick Coghlan
ncoghlan at iinet.net.au
Wed Feb 2 08:01:11 EST 2005
Nick Coghlan wrote:
> When not working at the hardware level, just go with the definition:
>
> Py> def complement(val, limit=256):
> ... if val >= limit or val < 0:
> ... raise ValueError("Value out of range for complemented format")
> ... if val == 0:
> ... return 0
> ... return limit - val
> ...
> Py> hex(complement(0x55))
> '0xab'
> Py> hex(complement(0x55, 256*256))
> '0xffab'
Or there's the simplest definition:
Py> def complement(val, limit=256):
... return (limit - val) % limit
...
Py> hex(complement(0x55))
'0xab'
Py> hex(complement(-0x55))
'0x55'
Py> hex(complement(0xab))
'0x55'
Py> hex(complement(-0xab))
'0xab'
Cheers,
Nick.
--
Nick Coghlan | ncoghlan at email.com | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.skystorm.net
More information about the Python-list
mailing list