Unicode Escape/Unescape
Escape and unescape Unicode text as JSON, JavaScript, Python, Java, CSS, HTML entities or UTF-8 percent-encoding, and inspect code points, UTF-8 bytes and hidden characters
Escape turns text into the chosen notation. Unescape detects \uXXXX, \u{X}, \UXXXXXXXX, \xHH, \n \t and numeric HTML entities by itself.
Text inspector
Escape text for the place it is going
A string has to survive a trip through something that only understands part of Unicode: a JSON
payload, a Java source file, a CSS content rule, an HTML attribute, a URL.
Each destination has its own notation, and using the wrong one is the usual reason an emoji
turns into two question marks or a build fails on a line that looks harmless. This tool
converts text into nine notations, converts them back, and shows what is actually inside a
string: code points, UTF-8 bytes, UTF-16 units, invisible characters, mixed scripts and
whether accents are stored composed or decomposed.
Pick the notation your destination actually parses
The same character, U+1F600 (😀), is written differently depending on who reads it. These are the forms the tool produces and where each one is valid:
- JSON string: only
\uXXXX, so 😀 becomes the surrogate pair\uD83D\uDE00. JSON has no\u{1F600}form. Quotes, backslashes and every control character below U+0020 must be escaped, which the JSON mode does for you (\n,\t,\u0001). - JavaScript / C#:
\uXXXXper UTF-16 unit, the same surrogate pair as JSON. - ES2015:
\u{1F600}for anything above U+FFFF, a single escape for a single character. - Python:
\U0001F600(capital U, exactly eight digits) above the Basic Multilingual Plane and\xHHfor control characters. - Java:
\uXXXXper UTF-16 unit. Control characters use\nand friends here, for the reason explained in the next section. - CSS:
\1F600, one to six hex digits. A space is added after the escape when the next character is a hex digit or whitespace, otherwise it would be read as part of the number. - HTML:
😀or😀. In this mode the characters& < > " 'are escaped too, so the output is safe to paste into markup, and so is the backslash, so that the unescape step can tell your text from a language escape. - UTF-8 percent-encoding:
%F0%9F%98%80, the form used in URLs. It keeps only the unreserved characters of RFC 3986 (letters, digits,- _ . ~), so it also encodes! ' ( ) *, whichencodeURIComponentleaves as they are.
Choose everything instead of non-ASCII only when you want every character escaped, for example to build test fixtures or to check that a parser really decodes escapes.
Four bugs this page helps you find
1. Python keeps surrogate halves as separate characters. In a Python 3
string literal, "\ud83d\ude00" is two characters, both lone surrogates, not 😀.
Calling .encode("utf-8") on it raises UnicodeEncodeError. The
json module, in contrast, does join the pair when it parses JSON. If you copy
\uD83D\uDE00 from a JSON or JavaScript log into Python source, use
\U0001F600 instead: this tool's Python mode writes it that way.
2. Java reads \uXXXX before it parses the string. A line such
as String s = "x\u000Ay"; is turned into a real line break inside the literal
and fails with unclosed string literal. Use \n; the Java mode does.
3. .length counts UTF-16 units, not characters.
"😀".length is 2 in JavaScript, while [..."😀"].length is 1. The
inspector shows the code point count, the UTF-16 unit count and the UTF-8 byte count side by
side, which explains most "my text is cut off at 255" and "the column is too short" reports.
4. Two identical-looking strings are not equal. é can be one
code point (U+00E9) or e plus a combining accent (U+0065 U+0301). In JavaScript
"é" === "e\u0301" is false, while after normalize("NFC")
it is true. A Cyrillic а (U+0430) inside a Latin word does the
same trick for security: "p\u0430ypal" === "paypal" is false. The
inspector reports whether the text is already in NFC or NFD and flags mixed scripts.
Reading the inspector
The table under the output has one row per code point (up to 200). UTF-8
is what is stored or sent over the network, UTF-16 is what JavaScript,
Java and C# index into, category is the Unicode general category (for
example Ll lowercase letter, So other symbol, Cf an
invisible format character), and script is the writing system.
Above the table, the tool lists suspicious characters it found: zero-width spaces
(U+200B, U+200C, U+200D), the byte order mark U+FEFF, no-break spaces, bidirectional
control characters such as U+202E that can reorder text on screen, control characters and
private-use characters. It also shows the number of user-perceived characters (grapheme
clusters) when your browser supports Intl.Segmenter, which is how a flag emoji
made of two code points still counts as one.
Unpaired surrogates (a lone \uD83D, for instance) are not valid characters in
UTF-8, HTML or percent-encoding. The tool warns you instead of silently replacing them.
When escaping is the wrong tool
If both ends handle UTF-8, send UTF-8. Escaping makes text harder to read and review, and
it does not make it safer: JSON.stringify leaves non-ASCII characters as they
are by default, while Python's json.dumps escapes them unless you pass
ensure_ascii=False. Both outputs are valid JSON and decode to the same text.
Escape when the channel is limited to ASCII or when a tool downstream mangles encodings.
Limits of this tool: it decodes numeric HTML entities but not named ones such as
é (use the HTML encode/decode tool for those); it does not decode
Python's \N{GRINNING FACE} names; CSS and percent-encoding are decoded only
when you select them, because a backslash or a percent sign can be plain text in other
contexts; and it shows normalization but does not change your text.
Code Examples
JavaScript: length, code points and normalization
console.log("😀".length, [..."😀"].length); // 2 1
console.log("\u{1F600}" === "\uD83D\uDE00"); // true
const composed = "\u00E9"; // é as one code point
const decomposed = "e\u0301"; // e + combining acute accent
console.log(composed === decomposed); // false
console.log(composed === decomposed.normalize("NFC")); // true
console.log(decomposed.length, decomposed.normalize("NFC").length); // 2 1Python: the surrogate pair trap
import json
s = "\ud83d\ude00" # two lone surrogates, not an emoji
print(len(s)) # 2
# s.encode("utf-8") # UnicodeEncodeError: surrogates not allowed
print(len("\U0001F600")) # 1 (this is the real emoji)
print(json.loads('"\\ud83d\\ude00"') == "\U0001F600") # True: json joins the pair
print(json.dumps({"city": "Zürich"})) # {"city": "Z\u00fcrich"}
print(json.dumps({"city": "Zürich"}, ensure_ascii=False)) # {"city": "Zürich"}Java: \u000A inside a string literal does not compile
// Does not compile: the compiler turns \u000A into a real line break
// String s = "x\u000Ay"; // error: unclosed string literal
String ok = "x\ny\uD83D\uDE00";
System.out.println(ok.length()); // 5 (UTF-16 units)
System.out.println(ok.codePointCount(0, ok.length())); // 4 (code points)Standards & Specifications
- RFC 8259 — JSON, section 7 (Strings) — Defines the \uXXXX escape, the surrogate pair form for characters above U+FFFF and which characters must be escaped
- ECMAScript — String literals — Specifies \uXXXX, \u{X} and \xHH escapes in JavaScript strings
- Java Language Specification, §3.3 Unicode Escapes — Explains why \uXXXX is processed before tokenization, which is the origin of the \u000A pitfall
- CSS Syntax Module Level 3 — Escaping — Defines CSS escapes of one to six hex digits and the optional trailing whitespace
- RFC 3986 — Percent-encoding — Defines percent-encoding and the unreserved character set; non-ASCII text is encoded as UTF-8 bytes
- Unicode Standard Annex #15 — Normalization Forms — Defines NFC and NFD, the source of the composed versus decomposed é difference
Frequently Asked Questions
Why does one emoji become two \u escapes?
JSON, JavaScript, Java and C# write strings as UTF-16 code units, and every character above U+FFFF needs two of them, called a surrogate pair. 😀 (U+1F600) is \uD83D\uDE00. If you need a single escape, choose the ES2015 format (\u{1F600}) or the Python format (\U0001F600).
Which format should I use inside a JSON file?
The JSON string format. JSON only allows \uXXXX (with surrogate pairs above U+FFFF), and requires quotes, backslashes and control characters below U+0020 to be escaped. The \u{...} form of JavaScript is not valid JSON.
Why does Python say 'surrogates not allowed' after I pasted \ud83d\ude00?
In a Python 3 string literal, \ud83d\ude00 is two separate lone surrogate characters, not one emoji, and they cannot be encoded to UTF-8. Write \U0001F600 instead, which is what the Python format of this tool produces. Python's json module does join the pair, but only when parsing JSON text.
Why does javac reject "\u000A" inside a string?
Java translates \uXXXX escapes before it tokenizes the source, so \u000A becomes a real line break inside the string literal and the string is unclosed. Use \n instead. The Java format of this tool writes control characters that way.
What does the 'unpaired surrogate' warning mean?
The text contains half of a surrogate pair (U+D800 to U+DFFF) with no partner. It is not a valid character in UTF-8, HTML or percent-encoding, so it cannot be converted faithfully. It usually comes from cutting a string in the middle of an emoji, for example with a UTF-16 substring.
Two strings look identical but are not equal. How do I find out why?
Paste both and read the inspector. It lists invisible characters (zero-width spaces, the byte order mark, no-break spaces, bidirectional controls), tells you whether the text is in NFC or NFD form (é as one code point or as e plus a combining accent), and warns when letters from different scripts are mixed, such as a Cyrillic а inside a Latin word.
Does the tool decode named HTML entities such as é?
No. It decodes numeric references (é and é) together with the escape notations of programming languages. For named entities use the HTML encode/decode tool.
Is my text sent to a server?
No. Escaping, unescaping and inspection run in your browser. You can confirm it in the network tab of your browser's developer tools: no request is made when you press the buttons.