ForHosting KIT · Developer Utilities

Printf format escaper

“Progress: 50% complete” becomes “Progress: 50%% complete”, so printf prints the percent sign instead of reading it as a conversion. Line breaks, tabs and backslashes are written as \n, \t and \\, and unescaping turns them back. Free in your browser.

  • Free
  • Runs in your browser
  • No sign-up
  • Your data stays on your device
Use it from WebAPIEmailTelegramApp soon
If you enter
Text
Progress: 50% complete next step
Calculation mode
Escape
You get
Result
Progress: 50%% complete\nnext step
Length
34
Percent count
1
Escape count
1

Printf-family format strings treat a bare percent sign as the start of a conversion specification, so free text that happens to contain a percent—progress labels, currency notes, log templates—will crash, mis-parse, or consume the wrong arguments unless every literal percent is doubled to %%. At the same time, format bodies often live inside source-code string literals or configuration files where newlines, tabs, and backslashes must be written as backslash escapes. This printf format escaper turns plain text into a safe format body by doubling every percent and rewriting control characters and backslashes as \\ , \n, \r, \t, or \xHH, and reverses the same map when you need the original characters back.

How printf percent doubling and backslash escapes work

In C printf, C++ iostreams that accept printf-style format strings, Python’s old % formatting, and many logging libraries, a percent character begins a conversion: %s, %d, %.2f, %.*s, and positional forms such as %1$s all start with a single percent. To print a literal percent you must write two of them in a row—%%—so the formatter emits one % and does not look for a conversion letter. Forgetting that rule is a classic bug: a message like "done 50%" becomes a format error or steals the next argument.

In escape mode it walks each character, replaces every % with %%, turns backslashes and the usual controls into the shortest portable escape, and leaves ordinary printable text unchanged. In unescape mode it folds %% back to a single percent, expands the supported backslash sequences, leaves a lone percent (the start of a real conversion) as-is so mixed format templates can be decoded, and rejects trailing backslashes, incomplete \x sequences, non-hex digits, and unknown letter escapes that would be undefined in a strict reading.

Where a printf format escaper earns its keep

Code generators that emit log templates, localization catalogs, and assert messages for C, C++, or printf-compatible runtimes must escape every free-text field before it is written into a format string, or a single percent in a product name will corrupt the format and break the build or crash at runtime. Embedded firmware and device-driver teams paste protocol dumps and operator messages that already contain %% and \n sequences and need an unescape path that fails loudly on truncated \xHH sequences instead of inventing bytes.

Documentation and cookbook sites that show copy-paste printf examples need a reliable escape path so user-supplied samples remain valid format bodies. Curriculum authors teaching printf syntax want examples that mention percent doubling explicitly, which is the search intent this landing captures. If you need full C string literals, JavaScript string escapes, HTML entities, or URL percent-encoding, treat those as separate search intents and landings rather than overloading this printf format escaper page.

Safe printf format bodies in generated code

Escape free-text titles, progress labels, and operator messages before writing them into printf-style format strings so every literal percent becomes %%.

Why does printf need percent signs to be doubled?

A single % starts a conversion such as %s or %d. Writing %% emits one literal percent and prevents the formatter from consuming arguments or erroring on a missing conversion.

Can I both escape and unescape?

Yes. Set mode to escape (default) to encode plain text into a printf-safe body, or mode to unescape to reverse percent doubling and supported backslash escapes.

What happens to a lone % when unescaping?

It is left as-is so real conversions like %s remain intact.

What does it cost?

The browser tool is free and runs locally.

What happens if an escape sequence is malformed?

Unescaping stops with a clear message instead of guessing. A trailing backslash, an incomplete \x sequence, a non-hex digit or an unknown letter after a backslash is refused, so no half-decoded bytes reach your output.

Everything on this page is available programmatically. This section is for teams who want to wire it into their own systems; everyone else can just use the tool above.

POSThttps://api.kit.forhosting.com/enc/printf-escape

Prefer to automate it? One authenticated POST creates the task; the result comes back by webhook or a signed link. The same capability also runs here on the web, by email and from Telegram — and soon from our app too.

curl -X POST https://api.kit.forhosting.com/enc/printf-escape \
  -H "Authorization: Bearer $KIT_KEY" \
  -H "Content-Type: application/json" \
  -d '{"text":"Progress: 50% complete\nnext: %s"}'
{
  "text": "Progress: 50% complete\nnext: %s"
}
{
  "task_id": "tsk_a1b2c3d4e5f6a1b2c3d4e5f6",
  "type": "enc.printf_escape",
  "status": "queued",
  "_links": {
    "result": "/tasks/tsk_…/result"
  }
}

The API is asynchronous: the call returns a task_id immediately and the result arrives by webhook. Polling is capped at 1 req/s per task.

Per request$0.002

Published price — no tokens, no invented credits. A failed task is never charged.

HTTPCodeMeaning
401unauthorizedMissing or invalid API key.
402insufficient_balanceYour balance doesn't cover the task price.
404unknown_typeThat task type doesn't exist.
429rate_limitedToo many requests. Use the webhook instead of polling.

Unescape understands %% and the listed backslash forms, leaves lone percent signs that introduce real conversions such as %s or %d untouched, and fails immediately with invalid_input on trailing backslashes, incomplete hex escapes, or unknown escape letters instead of inventing half-decoded garbage.

How printf percent doubling and backslash escapes work

Separately, when the same string is embedded in source code or JSON-like configs, a raw newline is often illegal or awkward, so authors write \n for a line feed, \t for a tab, \\ for a single backslash, and sometimes \xHH for other control bytes. This capability applies both layers deterministically. The path is pure string work with no network, no random seeds, and no wall-clock timestamps, so identical inputs always produce identical JSON. Pricing for successful API calls is the published base of $0.002 per item, and validation failures do not consume prepaid balance.

What you send and what you get back

Send a required string field named text and an optional mode field. Mode escape converts plain text into a printf-safe format body stored in result: every percent is doubled, backslashes become \\, and control characters become \n, \r, \t, or \xHH. Mode unescape reverses those sequences and returns the recovered string in result. Classic checks include escaping a progress line that contains a bare percent and a newline, unescaping a body that uses %% and \t, round-tripping a string that already contains backslashes, and rejecting a string that ends with a bare backslash. When mode is omitted, escape is the default so a single payload still does useful work. The response echoes mode and the original text, then includes result, the length of result, percent_count (how many percent signs were doubled on escape or undoubled on unescape, including the optional \% form), and escape_count (how many backslash-style transforms were applied). Missing text, non-string objects, unsupported mode values, strings longer than the documented maximum, trailing backslashes, incomplete hex escapes, non-hex digits after \x, and unknown escape letters all fail with invalid_input and a clear English message. Validation failures do not consume prepaid balance. Successful API pricing is the published base of $0.002 per item.

Where a printf format escaper earns its keep

Because this capability is a pure parse engine, API pricing is the published base of $0.002 per successful item and failed validation does not consume balance.

Decode format templates from logs and fixtures

Unescape printf-style bodies that use %% and backslash escapes back to readable text, with hard failures on incomplete or illegal escape sequences.

Golden vectors for format-string unit tests

Round-trip sample messages through escape and unescape in CI so percent doubling and control escapes stay stable across hosts and toolchains.

What happens if an escape sequence is malformed?

Unescape fails with invalid_input, naming the position and the bad sequence (for example a trailing backslash, incomplete \xHH, or an unknown letter escape). Nothing is charged for validation failures.

Read the full KIT documentation →