Added character encoding detection and conversion to escape()