# Re: how to separate hexadecimal

Discussion in 'Python' started by jrlen balane, Feb 2, 2005.

1. ### jrlen balaneGuest

On Wed, 02 Feb 2005 17:36:04 +1000, Nick Coghlan <> wrote:
> jrlen balane wrote:
> > i have a 4 digit hex number (2 bytes) and i want to separate it into 2
> > digit hex (1 byte each) meaning i want to get the upper byte and the
> > lower byte since i am going to add this two.
> > how am i going to do this?
> > should i treat it just like a normal string?
> >
> > ex. hexa = '0x87BE" # what i want to do is:
> > a = 0x87, b = 0xBE # so that i could do this:
> > c = a + b #which should be equal to 0x145

>
> divmod does what you want:
>
> Py> val = 0x87be
> Py> hi, lo = divmod(val, 0x100)
> Py> hex(hi), hex(lo)
> ('0x87', '0xbe')
> Py> hex(hi + lo)
> '0x145'
>
> Cheers,
> Nick.
>
> --
> Nick Coghlan | | Brisbane, Australia
> ---------------------------------------------------------------
> http://boredomandlaziness.skystorm.net
> --
> http://mail.python.org/mailman/listinfo/python-list
>

would i be able to perform bitwise operation with the result?
say, i want to get the two's complement of the result, is this correct:

twos_complement = (~ hex(hi + lo)) + 1

jrlen balane, Feb 2, 2005

2. ### Paul RubinGuest

jrlen balane <> writes:
> would i be able to perform bitwise operation with the result?
> say, i want to get the two's complement of the result, is this correct:
>
> twos_complement = (~ hex(hi + lo)) + 1

You can do bit operations, but hex(n) is the hex string for n, which
is not what you want.

If you want the hex form of the two's complement, just say

hex(-(hi+lo) & 0xff)

assuming you want one byte, or

hex(-(hi+lo) & 0xffff)

for two bytes.

Paul Rubin, Feb 2, 2005

3. ### Nick CoghlanGuest

Paul Rubin wrote:
> jrlen balane <> writes:
>
>>would i be able to perform bitwise operation with the result?
>>say, i want to get the two's complement of the result, is this correct:
>>
>>twos_complement = (~ hex(hi + lo)) + 1

>
>
> You can do bit operations, but hex(n) is the hex string for n, which
> is not what you want.
>
> If you want the hex form of the two's complement, just say
>
> hex(-(hi+lo) & 0xff)
>
> assuming you want one byte, or
>
> hex(-(hi+lo) & 0xffff)
>
> for two bytes.

More generally though, the "flip the bits and add one" of two's complement is
just a hardware implementation trick for "2*n - x".

When not working at the hardware level, just go with the definition:

Py> def complement(val, limit=256):
.... if val >= limit or val < 0:
.... raise ValueError("Value out of range for complemented format")
.... if val == 0:
.... return 0
.... return limit - val
....
Py> hex(complement(0x55))
'0xab'
Py> hex(complement(0x55, 256*256))
'0xffab'

Cheers,
Nick.

--
Nick Coghlan | | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.skystorm.net

Nick Coghlan, Feb 2, 2005
4. ### Nick CoghlanGuest

Nick Coghlan wrote:
> When not working at the hardware level, just go with the definition:
>
> Py> def complement(val, limit=256):
> ... if val >= limit or val < 0:
> ... raise ValueError("Value out of range for complemented format")
> ... if val == 0:
> ... return 0
> ... return limit - val
> ...
> Py> hex(complement(0x55))
> '0xab'
> Py> hex(complement(0x55, 256*256))
> '0xffab'

Or there's the simplest definition:

Py> def complement(val, limit=256):
.... return (limit - val) % limit
....
Py> hex(complement(0x55))
'0xab'
Py> hex(complement(-0x55))
'0x55'
Py> hex(complement(0xab))
'0x55'
Py> hex(complement(-0xab))
'0xab'

Cheers,
Nick.

--
Nick Coghlan | | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.skystorm.net

Nick Coghlan, Feb 2, 2005