invalid at invalid
Apr 27, 2012, 1:02 PM
Post #9 of 10
On 2012-04-27, Grant Edwards <invalid [at] invalid> wrote:
> On 2012-04-27, Grant Edwards <invalid [at] invalid> wrote:
>> On 2012-04-27, Paul Rubin <no.email [at] nospam> wrote:
>>> python <w.g.sneddon [at] gmail> writes:
>>>> What to decode hex '0xC0A8' and return signed short int.
>>> Is this right?
>>> n = int('0xC0A8', 16)
>>> if n >= 0xffff:
>>> n -= 0x10000
>> Yes, as long as the input value doesn't exceed 0x1ffff. This is
>> probably better:
>> n = int('0xc0a8'16) & 0xffff
> Oops, missed the "signed" part of the requirement.
> n = int('0xc0a8',16) & 0xffff
> if (n & 0x8000): n |= -1 & ~0xffff
if (n & 0x8000): n = -((~n & 0x7fff) + 1)
Grant Edwards grant.b.edwards Yow! I'm definitely not
at in Omaha!