Okay, so I made this little function that allows me to make a string into multiplier of 32 characters, but when I use String .replace I get some really, really weird bug. Since its making me pull my hair, can you guys take a look and see what I’m missing.
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$
It makes o in yo disappear. Ahhhh! But nothing disappears if I add some random numbers after.
SOLUTION – EDIT: My bad. I have misplaced self.filler and self.interrupt. I am so embarrassed now. The code should have been:
def unpad(self, data):
data = str(data).rstrip(self.filler)
return data.replace(self.interrupt, '')
Read the documentation for
rstrip:rstripremoves all trailing characters present in the passed set of characters. It doesn’t remove a trailing substring consisting of those characters in that order.'abczyx'.rstrip('xyz')gives'abc', and'abczyx'.rstrip('zyx')also gives'abc'.