encode function done