Re: hex to bin 16 bit word

2012-04-27 Thread Grant Edwards
On 2012-04-27, Grant Edwards wrote: > On 2012-04-27, Grant Edwards wrote: >> On 2012-04-27, Paul Rubin wrote: >>> python writes: What to decode hex '0xC0A8' and return signed short int. >>> >>> Is this right? >>> >>> n = int('0xC0A8', 16) >>> if n >= 0x: >>>n -= 0x1000

Re: hex to bin 16 bit word

2012-04-27 Thread Grant Edwards
On 2012-04-27, Grant Edwards wrote: > On 2012-04-27, Paul Rubin wrote: >> python writes: >>> What to decode hex '0xC0A8' and return signed short int. >> >> Is this right? >> >> n = int('0xC0A8', 16) >> if n >= 0x: >>n -= 0x1 > > Yes, as long as the input value doesn't ex

Re: hex to bin 16 bit word

2012-04-27 Thread MRAB
On 27/04/2012 20:32, Ian Kelly wrote: On Fri, Apr 27, 2012 at 12:56 PM, Paul Rubin wrote: python writes: What to decode hex '0xC0A8' and return signed short int. Is this right? n = int('0xC0A8', 16) if n>= 0x: n -= 0x1 No. n = int('0xC0A8', 16) if n>= 0x800

Re: hex to bin 16 bit word

2012-04-27 Thread Ian Kelly
On Fri, Apr 27, 2012 at 12:56 PM, Paul Rubin wrote: > python writes: >> What to decode hex '0xC0A8'  and return signed short int. > > Is this right? > >    n = int('0xC0A8', 16) >    if n >= 0x: >       n -= 0x1 No. n = int('0xC0A8', 16) if n >= 0x8000: n -= 0x1 -- http://mail.

Re: hex to bin 16 bit word

2012-04-27 Thread Paul Rubin
Steven D'Aprano writes: >> Is this right? >> >> n = int('0xC0A8', 16) >> if n >= 0x: >>n -= 0x1 > No. Oops, I meant n >= 0x7fff. Checking the sign bit. Grant Edwards writes: > Yes, as long as the input value doesn't exceed 0x1. This is > probably better: > > n

Re: hex to bin 16 bit word

2012-04-27 Thread Grant Edwards
On 2012-04-27, Paul Rubin wrote: > python writes: >> What to decode hex '0xC0A8' and return signed short int. > > Is this right? > > n = int('0xC0A8', 16) > if n >= 0x: >n -= 0x1 Yes, as long as the input value doesn't exceed 0x1. This is probably better: n = int(

Re: hex to bin 16 bit word

2012-04-27 Thread Steven D'Aprano
On Fri, 27 Apr 2012 11:56:45 -0700, Paul Rubin wrote: > python writes: >> What to decode hex '0xC0A8' and return signed short int. > > Is this right? > > n = int('0xC0A8', 16) > if n >= 0x: >n -= 0x1 No. >>> struct.unpack('>h',b'\xC0\xA8') (-16216,) >>> n = int('0xC0

Re: hex to bin 16 bit word

2012-04-27 Thread Paul Rubin
python writes: > What to decode hex '0xC0A8' and return signed short int. Is this right? n = int('0xC0A8', 16) if n >= 0x: n -= 0x1 -- http://mail.python.org/mailman/listinfo/python-list