code: def fmt_text(text):
      
      PRINTABLE_CHAR = set(
          list(range(ord(' '), ord('~') + 1)) + [ord('\r'), ord('\n')])
      newtext = ("\\x{:02X}".format(
          c) if c not in PRINTABLE_CHAR else chr(c) for c in text)
      textlines = "\r\n".join(l.strip('\r')
                              for l in "".join(newtext).split('\n'))
      return textlines
docs: convert characters that aren't printable to hex format