hex to bin 16 bit word

 Classic List Threaded
10 messages
Reply | Threaded
Open this post in threaded view
|
Report Content as Inappropriate

hex to bin 16 bit word

 Is there an other way to do this? What to decode hex '0xC0A8'  and return signed short int. >>> struct.unpack('>h','\xC0\xA8') (-16216,) Above works just wondering if there is cleaner way without writing a function. Bill -- http://mail.python.org/mailman/listinfo/python-list
Reply | Threaded
Open this post in threaded view
|
Report Content as Inappropriate

Re: hex to bin 16 bit word

 python <[hidden email]> writes: > What to decode hex '0xC0A8'  and return signed short int. Is this right?     n = int('0xC0A8', 16)     if n >= 0xffff:        n -= 0x10000 -- http://mail.python.org/mailman/listinfo/python-list
Reply | Threaded
Open this post in threaded view
|
Report Content as Inappropriate

Re: hex to bin 16 bit word

 On Fri, 27 Apr 2012 11:56:45 -0700, Paul Rubin wrote: > python <[hidden email]> writes: >> What to decode hex '0xC0A8'  and return signed short int. > > Is this right? > >     n = int('0xC0A8', 16) >     if n >= 0xffff: >        n -= 0x10000 No. >>> struct.unpack('>h',b'\xC0\xA8') (-16216,) >>> n = int('0xC0A8', 16) >>> if n >= 0xffff: ...     n -= 0x10000 ... >>> n 49320 Should be -16216. Personally, I don't see why the OP doesn't just use struct.unpack to unpack a struct. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Reply | Threaded
Open this post in threaded view
|
Report Content as Inappropriate

Re: hex to bin 16 bit word

 In reply to this post by Paul Rubin-7 On 2012-04-27, Paul Rubin <[hidden email]> wrote: > python <[hidden email]> 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 -- Grant Edwards               grant.b.edwards        Yow! What PROGRAM are they                                   at               watching?                               gmail.com             -- http://mail.python.org/mailman/listinfo/python-list
Reply | Threaded
Open this post in threaded view
|
Report Content as Inappropriate

Re: hex to bin 16 bit word

 In reply to this post by Steven D'Aprano-11 Steven D'Aprano <[hidden email]> 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 <[hidden email]> 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. -- http://mail.python.org/mailman/listinfo/python-list
Reply | Threaded
Open this post in threaded view
|
Report Content as Inappropriate

Re: hex to bin 16 bit word

 In reply to this post by Paul Rubin-7 On Fri, Apr 27, 2012 at 12:56 PM, Paul Rubin <[hidden email]> wrote: > python <[hidden email]> 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 -- http://mail.python.org/mailman/listinfo/python-list
Reply | Threaded
Open this post in threaded view
|
Report Content as Inappropriate

Re: hex to bin 16 bit word

 On 27/04/2012 20:32, Ian Kelly wrote: > On Fri, Apr 27, 2012 at 12:56 PM, Paul Rubin<[hidden email]>  wrote: >>  python<[hidden email]>  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 :-) -- http://mail.python.org/mailman/listinfo/python-list
Reply | Threaded
Open this post in threaded view
|
Report Content as Inappropriate

Re: hex to bin 16 bit word

 In reply to this post by Grant Edwards-7 On 2012-04-27, Grant Edwards <[hidden email]> wrote: > On 2012-04-27, Paul Rubin <[hidden email]> wrote: >> python <[hidden email]> 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 -- Grant Edwards               grant.b.edwards        Yow! It's a lot of fun                                   at               being alive ... I wonder if                               gmail.com            my bed is made?!? -- http://mail.python.org/mailman/listinfo/python-list
Reply | Threaded
Open this post in threaded view
|
Report Content as Inappropriate

Re: hex to bin 16 bit word

 On 2012-04-27, Grant Edwards <[hidden email]> wrote: > On 2012-04-27, Grant Edwards <[hidden email]> wrote: >> On 2012-04-27, Paul Rubin <[hidden email]> wrote: >>> python <[hidden email]> 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 or this:      if (n & 0x8000): n = -((~n & 0x7fff) + 1) -- Grant Edwards               grant.b.edwards        Yow! I'm definitely not                                   at               in Omaha!                               gmail.com             -- http://mail.python.org/mailman/listinfo/python-list
Reply | Threaded
Open this post in threaded view
|
Report Content as Inappropriate

Re: hex to bin 16 bit word

 snip Thanks for all you suggestions. This was sent via email.  Original solution with struct was not to bad but less clear that this was sent to me via email. >>> import ctypes >>> x = ctypes.c_int16(0xC08A) >>> x.value >>> -16246 -- http://mail.python.org/mailman/listinfo/python-list
Loading...