I need a function like string ToLiteral(string input) from this post. Such that
char *literal = to_literal("asdf\r\n");
Would yield literal ==> “asdf\\r\\n”.
I’ve googled around, but not been able to find anything (guess that I’ve must be using the wrong terms). However, I assume that a library with this functionality must be out there somewhere…
Thank you for the interresting answers. Googling “c string escape function” by the way seems to be the key to obtaining even more examples and GLIB provides g_strescape () which seems to be exactly what I need.
There’s no built-in function for this, but you could whip one up:
Note that I’ve left out translation of an escape sequence for the “escape” character, since this isn’t standardized in C (some compilers use
\eand others use\x). You can add in whichever applies to you.If you want a function that allocates your destination buffer for you: