MCPcopy Create free account
hub / github.com/ActiveState/code / encode

Function encode

recipes/Python/511432_base255_V1/recipe-511432.py:22–34  ·  view source on GitHub ↗

Encode a string to base 255.

(string, divide=1024)

Source from the content-addressed store, hash-verified

20################################################################################
21
22def encode(string, divide=1024):
23 'Encode a string to base 255.'
24 def encode(s):
25 i = 0
26 for c in s:
27 i *= 257
28 i += ord(c) + 1
29 s = ''
30 while i:
31 s = chr(i % 254 + 2) + s
32 i /= 254
33 return s
34 return '\1'.join(encode(string[index:index+divide]) for index in xrange(0, len(string), divide))
35
36def decode(string):
37 'Decode a string from base 255.'

Callers 1

recipe-121965.pyFile · 0.50

Calls 2

xrangeClass · 0.85
joinMethod · 0.45

Tested by

no test coverage detected