Re: how to separate hexadecimal

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

  1. jrlen balane

    jrlen balane Guest

    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?
    > > please help, thanks.
    > >
    > > 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
    #1
    1. Advertising

  2. jrlen balane

    Paul Rubin Guest

    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
    #2
    1. Advertising

  3. jrlen balane

    Nick Coghlan Guest

    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
    #3
  4. jrlen balane

    Nick Coghlan Guest

    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
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dmitry Bond.

    Hexadecimal formatting with leading zeros?!

    Dmitry Bond., Oct 13, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    7,960
    Oliver Sturm
    Oct 13, 2005
  2. Victor Hannak
    Replies:
    0
    Views:
    7,422
    Victor Hannak
    Feb 20, 2004
  3. EvgueniB
    Replies:
    1
    Views:
    611
    Anthony Borla
    Dec 15, 2003
  4. Frank Fredstone
    Replies:
    1
    Views:
    430
    Jean-Francois Briere
    Jun 27, 2006
  5. jrlen balane

    how to separate hexadecimal

    jrlen balane, Feb 2, 2005, in forum: Python
    Replies:
    1
    Views:
    258
    Paul Rubin
    Feb 2, 2005
Loading...

Share This Page