好的,所以我做了这个小函数,它允许我将一个字符串变成 32 个字符的乘数,但是当我使用 String .replace 时,我得到了一些非常非常奇怪的错误。既然它让我拉我的头发,你们能不能看看我错过了什么。
Variables:
self.blockSize = 32
self.interrupt = '$^EnD#Block^$'
self.filler = '#'
Functions:
def pad(self, data):
joint1 = ''.join([data, self.interrupt])
joint2 = self.filler * ((self.blockSize - len(joint1)) % self.blockSize)
return ''.join([joint1, joint2])
def unpad(self, data):
data = str(data).rstrip(self.interrupt)
return data.replace(self.filler, '')
Call:
p = e.pad('this is not a very good idea yo')
print(p)
print(e.unpad(p))
Output:
Jans-MacBook-Pro:test2 jan$ ../../bin/python3 data.py
this is not a very good idea yo123$^EnD#Block^$################
this is not a very good idea yo123
Jans-MacBook-Pro:test2 jan$ ../../bin/python3 data.py
this is not a very good idea yo$^EnD#Block^$###################
this is not a very good idea y
Jans-MacBook-Pro:test2 jan$
它使 o in yo 消失。啊啊啊!但是,如果我之后添加一些随机数,什么都不会消失。
解决方案 - 编辑:我的错。我放错了self.filler和self.interrupt。我现在好尴尬。代码应该是:
def unpad(self, data):
data = str(data).rstrip(self.filler)
return data.replace(self.interrupt, '')