Finished encode function to return string