CPU/Device/Codec.py
2024-05-02 08:54:42 +08:00

56 lines
1.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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)