I was working with pkcs#7 padding from and was dancing around between hex() and unhexlify to convert the padding size into a one byte character.

>>>binascii.unhexlify(bytes('0'+hex(4)[2:], encoding='utf-8')

In short, I must add a string zero in front if the size is less than 16. Very ugly. Then I ran into this function chr() and ord() that does what I needed. Convert a integer value into a character and back.

>>> chr(4)
>>> ord('\x04')

Simple and clean. The only thing is to make sure what encoding is the string I am appending this at the end so I can put it into the right encoding for it.

Additional References:


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s