Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

hex to bin 16 bit word

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


w.g.sneddon at gmail

Apr 27, 2012, 11:42 AM

Post #1 of 10 (330 views)
Permalink
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


no.email at nospam

Apr 27, 2012, 11:56 AM

Post #2 of 10 (331 views)
Permalink
Re: hex to bin 16 bit word [In reply to]

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
--
http://mail.python.org/mailman/listinfo/python-list


steve+comp.lang.python at pearwood

Apr 27, 2012, 12:09 PM

Post #3 of 10 (328 views)
Permalink
Re: hex to bin 16 bit word [In reply to]

On Fri, 27 Apr 2012 11:56:45 -0700, Paul Rubin 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


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


invalid at invalid

Apr 27, 2012, 12:11 PM

Post #4 of 10 (319 views)
Permalink
Re: hex to bin 16 bit word [In reply to]

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

--
Grant Edwards grant.b.edwards Yow! What PROGRAM are they
at watching?
gmail.com
--
http://mail.python.org/mailman/listinfo/python-list


no.email at nospam

Apr 27, 2012, 12:29 PM

Post #5 of 10 (320 views)
Permalink
Re: hex to bin 16 bit word [In reply to]

Steven D'Aprano <steve+comp.lang.python [at] pearwood> 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> 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


ian.g.kelly at gmail

Apr 27, 2012, 12:32 PM

Post #6 of 10 (319 views)
Permalink
Re: hex to bin 16 bit word [In reply to]

On Fri, Apr 27, 2012 at 12:56 PM, 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

No.

n = int('0xC0A8', 16)
if n >= 0x8000:
n -= 0x10000
--
http://mail.python.org/mailman/listinfo/python-list


python at mrabarnett

Apr 27, 2012, 12:43 PM

Post #7 of 10 (323 views)
Permalink
Re: hex to bin 16 bit word [In reply to]

On 27/04/2012 20:32, Ian Kelly wrote:
> On Fri, Apr 27, 2012 at 12:56 PM, 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
>
> 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


invalid at invalid

Apr 27, 2012, 12:57 PM

Post #8 of 10 (323 views)
Permalink
Re: hex to bin 16 bit word [In reply to]

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

--
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


invalid at invalid

Apr 27, 2012, 1:02 PM

Post #9 of 10 (322 views)
Permalink
Re: hex to bin 16 bit word [In reply to]

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

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


w.g.sneddon at gmail

Apr 28, 2012, 7:06 AM

Post #10 of 10 (320 views)
Permalink
Re: hex to bin 16 bit word [In reply to]

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

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.