ここに行きます:
#! /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.')
編集: 数値の 10 進数表現に 6 と 8 のシーケンスが多数含まれている場合は、10 進数表現の RLE を使用してみてください。おそらくハフマン ツリーと組み合わせてください。
EDIT 2 : (a) 6 と 8 のロングラン、および (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)