BCD List to HEX List
Philippe Martin
pmartin at snakecard.com
Sun Jul 30 19:09:16 EDT 2006
Philippe Martin wrote:
> Hi,
>
> I'm looking for an algo that would convert a list such as:
>
> 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
> l2 = func (l1)
> # l2 = [0x1, 0x2, 0xD, 0x6, 0x8, 0x7] #represents 0x12D687
>
>
> Regards,
>
> Philippe
Thanks to all,
I decided to attack the problem another way and change the code in device #2
so it can now take the output from device #1.
As device #2 only needs to compare, add, and subtract the stuff .. it makes
my life much simpler.
Sample (potentially buggy):
l1 = [1,2,3,4]
l2 = [0,2,3,9]
def sup (l1, l2): #assume same length
for i in range(len(l1) ):
if l1[i] > l2[i]:
return 1
if l1[i] < l2[i]:
return -1
return 0
def add (l1, l2): #assume same length
r = []
idx = range (len(l1))
idx.reverse()
carry = 0
for i in idx:
if l1[i] + l2[i] > 10:
carry = 1
r.insert(0,(l1[i] + l2[i]) % 10)
else:
r.insert(0,l1[i] + l2[i] + carry)
carry = 0
return r
print sup(l1,l2)
print add (l1,l2)
More information about the Python-list
mailing list