如果您只想存储一些信息但使用上面使用的符号集(0-9A-Z)“编码”它,您可以使用下面的算法。
该代码是我的一个旧 Python (3) 程序。这当然不是花哨的,也不是经过测试的,但我想总比没有好,因为你还没有得到很多答案。将代码移植到 PHP 或 AS 应该很容易。例如,reduce语句可以被命令式循环替换。另请注意,//表示 Python 中的整数除法。
对其进行一些压缩/加密也应该很容易。希望它和你想要的一样。开始。
from functools import reduce
class Coder:
    def __init__(self, alphabet=None, groups=4):
        if not alphabet:
            alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        self.alphabet = alphabet
        self.groups = groups
    def encode(self, txt):
        N = len(self.alphabet)
        num = reduce(lambda x,y: (x*256)+y, map(ord, txt))
        # encode to alphabet
        a = []
        while num > 0:
            i = num % N
            a.append(self.alphabet[i])
            num -= i
            num = num//N
        c = "".join(a)
        if self.groups > 0:
            # right zero pad
            while len(c) % self.groups != 0:
                c = c + self.alphabet[0]
            # make groups
            return '-'.join([c[x*self.groups:(x+1)*self.groups]
                             for x in range(len(c)//self.groups)])
        return c
    def decode(self, txt, separator='-'):
        # remove padding zeros and separators
        x = txt.rstrip(self.alphabet[0])
        if separator != None:
            x = x.replace(separator, '')
        N = len(self.alphabet)
        x = [self.alphabet.find(c) for c in x]
        x.reverse()
        num = reduce(lambda x,y: (x*N)+y, x)
        a = []
        while num > 0:
            i = num % 256
            a.append(i)
            num -= i
            num = num//256
        a.reverse()
        return ''.join(map(chr, a))
if __name__ == "__main__":
    k = Coder()
    s = "Hello world!"
    e = k.encode(s)
    print("Encoded:", e)
    d = k.decode(e)
    print("Decoded:", d)
示例输出:
Encoded: D1RD-YU0C-5NVG-5XL8-7620
Decoded: Hello world!