开始:
#! /usr/bin/python
n = 313105074639950943116 #just an example
#your algorithm
chars = []
buff = ''
s = str (n)
while s:
if int (buff + s [0] ) < 256:
buff += s [0]
s = s [1:]
else:
chars.append (int (buff) )
buff = ''
if buff: chars.append (int (buff) )
print ('You need to write these numbers converted to chars: {}'.format (chars) )
print ('This are {} bytes of data.'.format (len (chars) ) )
print ('But you cannot decompress it, because you lose leading zeros.')
chars = []
while n:
chars.append (n & 0xff)
n = n >> 8
print ('Now if you just write the number to a file without your algorithm:')
print ('You need to write these numbers converted to chars: {}'.format (chars) )
print ('This are {} bytes of data.'.format (len (chars) ) )
print ('And you can actually read it again.')
编辑:如果您的数字的十进制表示有很多 6s 和 8s 序列,您应该尝试使用十进制表示的 RLE,可能与 Huffman 树结合使用。
编辑 2:考虑到 (a) 6s 和 8s 的长时间运行,以及 (b) 你不想使用一些已建立的算法的事实,你可以使用一些非常粗糙的 RLE,如下所示:
#! /usr/bin/python
n = 313666666666666688888888888888888866666666666666666666666666666610507466666666666666666666666666399509431888888888888888888888888888888888888888888881666666666666
s = str (n)
print (s)
comp = ''
count = None
while s:
if s [0] in '01234579':
if count:
comp += ('<{}>' if count [0] == 6 else '[{}]').format (count [1] )
count = None
comp += s [0]
if s [0] == '6':
if count and count [0] == 6: count = (6, count [1] + 1)
elif count:
comp += ('[{}]').format (count [1] )
count = (6, 1)
else: count = (6, 1)
if s [0] == '8':
if count and count [0] == 8: count = (8, count [1] + 1)
elif count:
comp += ('<{}>').format (count [1] )
count = (8, 1)
else: count = (8, 1)
s = s [1:]
if count: comp += ('<{}>' if count [0] == 6 else '[{}]').format (count [1] )
print (comp)