Improve conversion from utf16 to utf8

Also use proper method to get string length of utf8 strings.
Using strlen sometimes returns the wrong length.

Change-Id: Ib2b125afa54622c55f74f3917e299bad5c1c769a