Philippe Martin wrote: > John Machin wrote: > > > > > Philippe Martin wrote: > >> 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. > >> > > > > I'm confused. > > 1. Was the original device #1 or #2? > > 2. How many bits does the non-original device's C compiler support? > > 3. If the original device is device #1, please explain where *it* > > obtained an 8-digit decimal number expressed as 1 digit per byte (or > > int) ... > > > Well I don't want to bore you guys more than needed ;-) but: > > Device #1 has an 8 bit processor - uses a C cross-compiler that does not > know anything above a 16 bit integer. I use this device to get information > from users "1234...". > > Device #2 has an 8 bit processor - uses a subset of Java ... that does not > know anything above a 16 bit integer. > > > The information gathered in device number #1 must then be sent to device #2 > (after being encrypted .... ) to be compared, subtracted or added. > > The code I already have in device #2 makes the assumption that the > information received is an array of bytes of length N which represents an > actual value. ex: 0x67DF5 ==> [0x6, 0x7, 0xD, 0xF, 0x5] ... so it can > compare/add/subtract values ... and do its job. > > As a python fan, I figured (back to my initial not very clear request), that > I could prototype the above without making any major assumption as to the > capabilities of the interpreter. > > > I still believe that to be true. >
Try this: C:\junk>type bcd.py def reconstitute_int(alist): reghi, reglo = 0, 0 for digit in alist: assert 0 <= digit <= 9 reghi, reglo = mul32by10(reghi, reglo) reghi, reglo = add32(reghi, reglo, 0, digit) return reghi, reglo def uadd16(a, b): return (a + b) & 0xFFFF def shr32by4(hi, lo): newhi = (hi >> 4) & 0xFFFF newlo = ((lo >> 4) | ((hi & 0xF) << 12)) & 0xFFFF return newhi, newlo def add32(hia, loa, hib, lob): lox = uadd16(loa, lob) hix = uadd16(hia, hib) inx = ((lox & 0x8000) >> 13) + ((lob & 0x8000) >> 14) + ((loa & 0x8000) >> 1 5) carry = [0, 1, 1, 1, 0, 0, 0, 1][inx] # The above is admittedly ugly but sheesh I haven't even had my # second cup of coffee yet today :-) # Anybody who's good at solving equations in Boolean algebra, # pls beautify this!! if carry: hix = uadd16(hix, 1) expected = (hia+hib)*65536 + loa + lob actual = hix*65536 + lox if actual != expected: print (hia, loa), (hib, lob), (hix, lox), actual, expected, inx, carry return hix, lox def mul32by10(hi, lo): tmphi, tmplo = add32(hi, lo, hi, lo) # 2 times tmphi, tmplo = add32(tmphi, tmplo, tmphi, tmplo) # 4 times tmphi, tmplo = add32(tmphi, tmplo, hi, lo) # 5 times tmphi, tmplo = add32(tmphi, tmplo, tmphi, tmplo) # 10 times return tmphi, tmplo def make_hex32(aninthi, anintlo): result = [] while aninthi or anintlo: result.append(anintlo & 0xF) aninthi, anintlo = shr32by4(aninthi, anintlo) return result def reverse_list(alist): n = len(alist) for i in xrange(n >> 1): reg1 = alist[n - 1 - i] reg2 = alist[i] alist[i] = reg1 alist[n - 1 - i] = reg2 C:\junk>python Python 2.4.3 (#69, Mar 29 2006, 17:35:34) [MSC v.1310 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import bcd >>> num = bcd.reconstitute_int([1,2,3,4,5,6,7,8]) >>> num (188, 24910) >>> num[0]*65536 + num[1] 12345678 >>> result = bcd.make_hex32(*num) >>> result [14, 4, 1, 6, 12, 11] >>> bcd.reverse_list(result) >>> result [11, 12, 6, 1, 4, 14] >>> ['0x%x' % digit for digit in result] ['0xb', '0xc', '0x6', '0x1', '0x4', '0xe'] >>> ^Z -- http://mail.python.org/mailman/listinfo/python-list