BCD List to HEX List

Philippe Martin pmartin at snakecard.com
Sun Jul 30 18:07:57 EDT 2006


Paul Rubin wrote:

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

Thanks Paul,

I'm just using Python to prototype, so I cannot use any of these great
features of the language.

Regards,

Philippe




More information about the Python-list mailing list