CHR and ORD

I was working with pkcs#7 padding from cryptopals.com 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')
b'\x04'

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)
'\x04'
>>> ord('\x04')
4

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:

Advertisements