def dec2bin(n, count=28): """returns the binary of integer n, using count number of digits""" return "".join([str((n >> y) & 1) for y in range(count-1, -1, -1)]) # put the two demos in 'initparta' and 'initpartb' initparta = '64:35791394|35791394|35791394|35840546|35791394|35791394|35791394|35791394|35791394|2' parta = initparta.split(':')[-1] initpartb = '64:35791394|35791394|36577826|35791394|35791394|35791394|35791394|35791394|35791394|2' partb = initpartb.split(':')[-1] partalist = parta.split('|') # start with all but end of demo 1 demo = [] for decset in partalist[0:-1]: binset = dec2bin(int(decset)) # split the set set = [] for x in range(7): set.append(binset[x*4:x*4+4]) demo.append(set) #do final piece of demo 1 decset = int(partalist[-1]) binset = dec2bin(decset) # split the set set = [] for x in range(7): set.append(binset[x*4:x*4+4]) # find the offset count = 0 for x in set: if x != '0000': offset = count break count += 1 # add to the demo set.sort(reverse=True) #demo.append(set[offset:7]) demo.append(set[0:-offset]) #print set[offset:7] # on to demo 2 allframes = partb.split('|') for decset in allframes: binset = dec2bin(int(decset)) # split the set set = [] for x in range(7): set.append(binset[x*4:x*4+4]) # add to the demo set.sort(reverse=True) demo[-1] += set[0:offset] demo.append(set[offset:7]) # compose decimalised demo finaldemo = '' for x in demo: set = '' for y in x: set += y finaldemo += str(int(set,2)) finaldemo += "|" finaldemo = finaldemo[0:-1] # get the length length = 0 for x in finaldemo.split('|'): binset = dec2bin(int(x)) # split the set set = [] for x in range(7): set.append(binset[x*4:x*4+4]) # find the offset for x in set: if x != '0000': length += 1 # final output print initparta print '' print '+' print '' print initpartb print '' print '=' print '' print str(length) + ':' + finaldemo