how to separate hexadecimal

Paul Rubin http
Wed Feb 2 02:35:46 EST 2005


jrlen balane <nbbalane at gmail.com> writes:
> 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

Assuming you really want hexa to begin with the characters '0x', the
string slicing way is:

    a, b = hexa[2:4], hexa[4:6]   # a = '87', b = 'BE'
    c = int(a,16) + int(b, 16)

A more direct arithmetic way is:

   x = int(hexa, 16)     # x = the integer 0x87be
   c = (x >> 8) + (x & 0xff)



More information about the Python-list mailing list