BCD List to HEX List

Paul Rubin http
Sun Jul 30 18:08:29 EDT 2006


Philippe Martin <pmartin at snakecard.com> writes:
> I'm using python to prototype the algo: this will move to C in an embedded
> system where an int has 16 bits - I do not wish to use any python library.
> 
> l1 = [1,2,3,4,6,7,8] #represents the decimal number 12345678

This is untested, but should give you the idea:

First, convert that list to a decimal digit string:

   s = ''.join(map(str, l1))

Then convert the string to an integer:

   n = int(s)    # base 10 is the default

Then convert the integer to a hex digit string:

   h = '%X' % n

Finally, convert the hex digit string to a list of integer values of the
individual digits:

   vlist = [int(d, 16) for d in h]

This is the list you want.

If you prefer, You can do it all in one line:

   vlist = [int(d, 16) for d in ('%X' % int(''.join(map(str, l1))))]



More information about the Python-list mailing list