56 lines
1.6 KiB
Python
56 lines
1.6 KiB
Python
from functools import lru_cache
|
||
import math
|
||
def str2int(dt: str):
|
||
a = []
|
||
maxInt = 0
|
||
# 遍历输入的字符串
|
||
for i in dt:
|
||
# 使用ord函数将每个字符转换为对应的ASCII码,生成整数流
|
||
this = ord(i)
|
||
if this >maxInt:
|
||
maxInt = this
|
||
a.append(str(this))
|
||
IntSize = len(str(maxInt))
|
||
out = str(IntSize)
|
||
for i in a:
|
||
i.zfill(IntSize)
|
||
# 将处理后的整数字符串添加到输出字符串中
|
||
out += i
|
||
return int(out)
|
||
|
||
def int2str(dt: list[int]):
|
||
# 遍历整数列表
|
||
a = []
|
||
for i in dt:
|
||
# 使用chr函数将每个整数转换为对应的字符,生成字节流
|
||
a.append(chr(i))
|
||
return a
|
||
|
||
def list2int(dt: list[str]):
|
||
# 防止第一个数为零
|
||
maxInt = max(dt)
|
||
IntSize = len(str(maxInt))
|
||
out = str(IntSize)
|
||
|
||
# 遍历整数列表
|
||
for i in dt:
|
||
# 将整数转换为字符串
|
||
i = str(i)
|
||
# 如果字符串长度小于3,则在前面补0,使其长度为3
|
||
if len(i) < IntSize:
|
||
while len(i) < IntSize:
|
||
i = '0' + i
|
||
# 将处理后的整数字符串添加到输出字符串中
|
||
out += i
|
||
# 将结果字符串转换为整数并返回
|
||
return int(out)
|
||
|
||
def int2list(dt: int):
|
||
# 将整数转换为字符串,并去掉前面的'100'
|
||
size = int(str(dt)[0])
|
||
dt = str(dt)[1:]
|
||
# 将字符串按照每3个字符分割为子列表
|
||
sub_lists = [dt[i:i+size] for i in range(0, len(dt), size)]
|
||
# 遍历子列表并将其转换为整数后返回
|
||
for i in sub_lists:
|
||
yield int(i) |