encode function