将数字转换为字符串

2024-09-27 23:26:04 发布

您现在位置:Python中文网/ 问答频道 /正文

如果我有一个字符串s='ABCDEFJHI',然后像这样分割它['ABC','DEF','JHI']。 我有一个函数encode(一些计算),它将切片字符串转换成数字。在

for example 'encode('ABC' ) gives 50 , encode('DEF') gives 33, encode('JHI') gives 10

['ABC','DEF','JHI']给出[50,33,10]。 我想做相反的情况,解码(50)给出'ABC' 我有一个想法,当我编码子字符串时,我创建一个库,然后用它的编号附加子字符串,比如:('ABC':50)(对所有子字符串都做同样的操作),稍后在解码中,我将根据编号提取子字符串。 如何在python中实现这一点?在


Tags: 函数字符串forexampledef情况切片数字
2条回答

如果它是可逆的,我建议以相反的格式存储(50:'ABC')。同时,想象一个给定代码以前没有编码的情况。在

encode_history = {}

def encode(str):
    """some calculations which lead to the code"""
    ... your calculations ...
    encode_history[code] = str
    return code


def decode(code):
    """function to convert a code to string"""
    if code in encode_history:
        return encode_history[code]
    else:
        return None

在您的编码函数中:

def encode(the_string):
    #do whatever encoding you're doing
    return (the_number,the_string)

无论您在哪里使用它,请:

^{pr2}$

定义函数也类似于:

def decode(lookup_table,value):
    return lookup_table[value]

使用方式如下:

^{4}$

那就是为什么你要这么做,你是怎么做的,为什么你不使用某种真正的加密技术?如果不是双向加密,那么几乎可以肯定的是,您不应该在任何地方存储数据,如果是双向加密,为什么不使用与加密相反的算法来解密它呢?记住这一点。。。。。在

相关问题 更多 >

    热门问题