Quantcast

hex to bin 16 bit word

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

hex to bin 16 bit word

w.g.sneddon@gmail.com
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
star

Re: hex to bin 16 bit word

Paul Rubin-7
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
star

Re: hex to bin 16 bit word

Steven D'Aprano-11
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
star

Re: hex to bin 16 bit word

Grant Edwards-7
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
star

Re: hex to bin 16 bit word

Paul Rubin-7
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
star

Re: hex to bin 16 bit word

Ian Kelly-2
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
star

Re: hex to bin 16 bit word

MRAB-2
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
star

Re: hex to bin 16 bit word

Grant Edwards-7
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
star

Re: hex to bin 16 bit word

Grant Edwards-7
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
star

Re: hex to bin 16 bit word

w.g.sneddon@gmail.com
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...