Perhaps you want strutils.escape? I.e.:
import strutils
echo escape("\n\t")
Output is "\x0A\x09"
Its output is designed to be as portable an 'input' to de-escapers/escape-interpreters as possible. So you will see things like \x0A and \x09 instead of the \n \t because the former syntax is accepted by more string parsers.
no, I am asking for the inverse of that function. There is unescape, but it doesn't do what I want to do:
import strutils
let str = r"\n\r"
echo str
assert unescape(str, "", "") == "\n\r"