Consolidated UTF-conversion code.

  This avoids duplication and updates #148.