Don't crash if we fail to determine character encoding

Fixes #1947