The C string literal escaper and unescaper converts arbitrary text into the exact content you can place between the quotation marks of a C or C++ string literal, and turns escaped literal content back into readable text. It is essential when embedding paths, JSON, multi-line messages or binary data in C source.
How it works
Escaping follows the C standard. The backslash and double quote are escaped to \\ and \" so they do not terminate or corrupt the literal. The familiar control characters map to named escapes: newline is \n, tab is \t, the alert/bell byte is \a, and so on. Any remaining byte below 32, plus DEL, is written as a hex escape \xHH. Characters outside ASCII are first encoded to their UTF-8 bytes, and each byte becomes its own \xHH, keeping the literal seven-bit clean.
Unescaping is the reverse and is byte oriented so it can reconstruct UTF-8 correctly. It recognises the named escapes, hex escapes \xH... (greedy on hex digits, as the C standard specifies), and octal escapes of up to three digits such as \101. Unknown escapes drop the backslash and keep the following character. The collected bytes are decoded as UTF-8 at the end.
Example
Escaping a Windows path with an embedded newline and quotes:
Path: C:\\dir\nLine \"two\"
Note that the single backslash in C:\dir becomes \\, the newline becomes \n, and the inner quotes become \". Unescaping that content restores the original text exactly.
All named escape sequences
The C standard defines these single-character escapes:
| Sequence | Meaning | ASCII code |
|---|---|---|
\n | Newline (line feed) | 10 |
\r | Carriage return | 13 |
\t | Horizontal tab | 9 |
\v | Vertical tab | 11 |
\f | Form feed | 12 |
\b | Backspace | 8 |
\a | Alert (bell) | 7 |
\\ | Backslash | 92 |
\" | Double quote | 34 |
\' | Single quote | 39 |
\0 | Null byte | 0 |
Any byte that does not have a named escape and is not a printable ASCII character is written as \xHH (two uppercase hex digits).
Common use cases
Embedding file paths. Windows paths use backslashes, which must be doubled: C:\Users\name becomes C:\\Users\\name in a C string literal. Forgetting this is the most common cause of path-related string bugs on Windows.
Embedding JSON in a C program. JSON uses double quotes, backslashes, and newlines — all of which need escaping. Pasting JSON into this tool produces the exact escaped form for use in a const char* variable or a printf call, without the risk of manual escaping errors.
Binary data in test fixtures. When writing unit tests for parsers or binary protocols, it is sometimes useful to embed a small binary payload as a C string literal with \xHH escapes. The escaper generates these sequences for any byte value, and the unescaper can decode them back for verification.
Multi-line strings. A multi-line message becomes a series of \n-terminated segments that concatenate at link time using C’s adjacent string-literal concatenation: "Line one\n" "Line two\n". The escaper converts your newlines to \n and you add the quote boundaries manually.
The hex-escape greediness trap
C hex escapes consume as many hex digits as follow the \x. This means "\x41B" is not "A" followed by "B" — it is a single escape with value 0x41B, which overflows a char. The safe solutions are to split the string: "\x41" "B", or use the equivalent octal escape \101 (which is at most three octal digits and does not suffer from greediness), or use the \u universal-character-name syntax in C++ for Unicode code points.
Notes
- Hex escapes in C are greedy:
\x41is the letter A, but\x4142would be one (overflowing) value, so prefer octal or splitting strings when ambiguity matters. - The output deliberately omits the surrounding quotes so you control the literal in your own code.
- A non-ASCII character such as é is emitted as its two UTF-8 bytes
\xc3\xa9. - C++ raw string literals (
R"(...)") are an alternative when embedding strings with many backslashes or quotes, as they require no escaping.