hex to bin 16 bit word

Paul Rubin no.email at nospam.invalid
Fri Apr 27 15:29:23 EDT 2012


Steven D'Aprano <steve+comp.lang.python at pearwood.info> writes:
>> Is this right?
>> 
>>     n = int('0xC0A8', 16)
>>     if n >= 0xffff:
>>        n -= 0x10000
> No.

Oops, I meant n >= 0x7fff.  Checking the sign bit.  

Grant Edwards <invalid at invalid.invalid> writes:
> Yes, as long as the input value doesn't exceed 0x1ffff.  This is
> probably better:
>
>  n = int('0xc0a8'16) & 0xffff

Yeah, I was relying on the specification that the input was 16 bits.



More information about the Python-list mailing list