new encode function