fixed write_to_string to support binary content