replace invalid chars on encode