Fixed encode and decode functions to always use bytearray for input.