hex to bin 16 bit word

MRAB python at mrabarnett.plus.com
Fri Apr 27 15:43:31 EDT 2012


On 27/04/2012 20:32, Ian Kelly wrote:
> On Fri, Apr 27, 2012 at 12:56 PM, Paul Rubin<no.email at nospam.invalid>  wrote:
>>  python<w.g.sneddon at gmail.com>  writes:
>>>  What to decode hex '0xC0A8'  and return signed short int.
>>
>>  Is this right?
>>
>>      n = int('0xC0A8', 16)
>>      if n>= 0xffff:
>>         n -= 0x10000
>
> No.
>
> n = int('0xC0A8', 16)
> if n>= 0x8000:
>      n -= 0x10000

Or:

n = int('0xC0A8', 16)
n -= (n & 0x8000) * 2

:-)



More information about the Python-list mailing list