encode to utf8 if necessary first