Escape/Unescape Unicode
Escape e unescape caracteres Unicode para diversos contextos de programação
What Is Unicode Escaping?
Unicode escaping converts characters into their code point representations using language-specific escape sequences. Every character in the Unicode standard is assigned a unique code point written as U+XXXX (for the Basic Multilingual Plane) or U+XXXXX/U+XXXXXX (for supplementary planes). Escape sequences allow developers to embed any Unicode character in source code, configuration files, and data streams — even characters that cannot be typed directly on a keyboard or would break string delimiters. This tool converts between literal characters and their escaped representations across JavaScript, Python, Java, Go, and other languages, handling the full Unicode range from U+0000 to U+10FFFF.
Unicode Code Points and the BMP
Unicode organizes characters into 17 planes of 65,536 code points each. Plane 0 (U+0000 to U+FFFF) is the Basic Multilingual Plane (BMP) and contains the vast majority of commonly used characters — Latin, Greek, Cyrillic, CJK ideographs, Arabic, Hebrew, and most symbols. Planes 1 through 16 (U+10000 to U+10FFFF) are supplementary planes, hosting emoji, historic scripts, musical notation, and rare CJK characters.
The distinction matters because many escape formats were designed when only the BMP existed.
The classic \uXXXX escape sequence uses exactly four hex digits and can only
represent code points up to U+FFFF. Characters outside the BMP require either surrogate pairs
(in UTF-16-based languages) or extended escape syntax. Understanding this boundary is essential
when working with emoji, mathematical symbols, or multilingual text that includes supplementary
characters.
Code points are abstract numbers — they do not dictate byte representation. The same code point
U+1F600 (😀) is encoded as 4 bytes in UTF-8 (F0 9F 98 80), as a surrogate pair in
UTF-16 (D83D DE00), and as a single 32-bit unit in UTF-32 (0001F600).
Escape sequences operate at the code point level, not the byte level, which is why they are
portable across encodings.
Escape Formats by Language
Different programming languages use distinct escape syntaxes to represent Unicode characters in string literals:
- JavaScript (ES5):
\uXXXX— four hex digits, BMP only. For supplementary characters, use a surrogate pair:\uD83D\uDE00for U+1F600. - JavaScript (ES6+):
\u{XXXXX}— curly brace syntax supports 1 to 6 hex digits, covering the full Unicode range without surrogate pairs:\u{1F600}. - Python:
\uXXXXfor BMP characters and\U00XXXXXX(capital U, exactly 8 hex digits) for any code point:\U0001F600. Python also supports\N{name}for named characters. - Java:
\uXXXX— strictly four hex digits. Java uses UTF-16 internally, so supplementary characters require surrogate pairs:\uD83D\uDE00. Java does not have a curly-brace escape. - Go:
\uXXXXfor BMP and\UXXXXXXXX(capital U, exactly 8 hex digits) for supplementary code points. Go strings are UTF-8, but escape sequences use code points. - C#:
\uXXXXfor BMP and\UXXXXXXXXfor supplementary characters. Identical to Go syntax. - HTML/XML:
&#xXXXX;or&#decimal;— numeric character references work for any code point:😀or😀. - CSS:
\XXXXfollowed by a space — a backslash then 1 to 6 hex digits:\1F600(trailing space separates from subsequent characters).
This tool detects the input format automatically and converts between these representations, preserving ASCII characters unchanged and escaping only non-ASCII code points by default.
Surrogate Pairs and UTF-16 Encoding
UTF-16 encodes code points from U+0000 to U+FFFF as a single 16-bit code unit, but supplementary characters (U+10000 to U+10FFFF) require two 16-bit units called a surrogate pair. The first unit is a high surrogate (range U+D800 to U+DBFF) and the second is a low surrogate (range U+DC00 to U+DFFF). Together they encode the full code point using the formula:
codePoint = (highSurrogate - 0xD800) × 0x400 + (lowSurrogate - 0xDC00) + 0x10000
Languages that use UTF-16 internally (JavaScript, Java, C#) expose surrogate pairs in their
string indexing. For example, in JavaScript, "😀".length returns 2 because the
string contains two UTF-16 code units. The ES6 \u{1F600} escape avoids this
complexity by working directly with code points, but the underlying storage still uses
surrogate pairs.
When escaping text for UTF-16-based languages without curly-brace support (Java, ES5 JavaScript), this tool automatically generates the correct surrogate pair for any supplementary character. When unescaping, it correctly recombines surrogate pairs back into a single code point.
UTF-16 vs UTF-32 Encoding Differences
UTF-16 and UTF-32 are two of the three standard Unicode encoding forms (alongside UTF-8). Their fundamental difference lies in code unit size and whether variable-length encoding is used:
- UTF-16: Uses 16-bit (2-byte) code units. BMP characters use one code unit; supplementary characters use two (a surrogate pair). This makes string length and character indexing non-trivial for text containing emoji or rare scripts.
- UTF-32: Uses 32-bit (4-byte) code units. Every character uses exactly one code unit, making random access O(1) and string length equal to character count. However, it uses 2 to 4 times more memory than UTF-8 for typical text.
Escape sequences abstract away these encoding differences — \u{1F600} in
JavaScript and \U0001F600 in Python both denote the same code point regardless
of the runtime encoding. However, understanding the underlying encoding helps when debugging
issues like incorrect string lengths, broken emoji rendering, or corrupt data from mismatched
encoding assumptions. This tool displays both the code point and UTF-16 code unit values to
help developers understand exactly how their text is represented in memory.
Code Examples
Unicode escape and unescape in multiple languages
// JavaScript — ES6 curly brace escape (full Unicode range)
const emoji = "\u{1F680}"; // 🚀 (U+1F680 ROCKET)
const heart = "\u{2764}"; // ❤ (U+2764 HEAVY BLACK HEART)
// ES5 surrogate pair for the same rocket emoji
const rocketES5 = "\uD83D\uDE80"; // 🚀 (surrogate pair)
// Convert character to its escape sequence
function toUnicodeEscape(char) {
const cp = char.codePointAt(0);
if (cp > 0xFFFF) {
// Supplementary character — show both formats
const hi = Math.floor((cp - 0x10000) / 0x400) + 0xD800;
const lo = ((cp - 0x10000) % 0x400) + 0xDC00;
return {
es6: `\\u{${cp.toString(16).toUpperCase()}}`,
es5: `\\u${hi.toString(16).toUpperCase()}\\u${lo.toString(16).toUpperCase()}`,
python: `\\U${cp.toString(16).toUpperCase().padStart(8, '0')}`,
};
}
const hex = cp.toString(16).toUpperCase().padStart(4, '0');
return {
es6: `\\u{${hex}}`,
es5: `\\u${hex}`,
python: `\\u${hex}`,
};
}
console.log(toUnicodeEscape("🚀"));
// { es6: "\\u{1F680}", es5: "\\uD83D\\uDE80", python: "\\U0001F680" }Unicode escaping in Python and Go
# Python — \uXXXX for BMP, \UXXXXXXXX for supplementary
greeting = "\u0048\u0065\u006C\u006C\u006F" # "Hello"
rocket = "\U0001F680" # 🚀
# Get escape sequences from characters
char = "🎉"
code_point = ord(char)
print(f"U+{code_point:04X}") # U+1F389
print(f"Python: \\U{code_point:08X}") # \U0001F389
print(f"HTML: &#x{code_point:X};") # 🎉
# Go equivalent (in string literals):
# rocket := "\U0001F680" // 🚀
# star := "\u2B50" // ⭐ (BMP character)Standards & Specifications
- The Unicode Standard — Defines all code points, planes, and encoding forms (UTF-8, UTF-16, UTF-32)
- ECMAScript 2023 — String Literals — Specifies \uXXXX and \u{XXXXX} escape syntax for JavaScript strings
- RFC 3629 (UTF-8) — Defines UTF-8 encoding — the most common Unicode encoding on the web
Perguntas Frequentes
What is Unicode escaping and why is it used?
Unicode escaping converts characters to \uXXXX format, where XXXX is the hexadecimal code point. It's used in programming to represent special characters, non-ASCII text, and emoji in source code that might not support UTF-8 encoding. For example, 'é' becomes '\u00E9'. This ensures code portability across different systems and editors.
How do surrogate pairs work for emoji?
Characters beyond U+FFFF (like emoji) require surrogate pairs - two \uXXXX sequences. For example, 😀 (U+1F600) becomes '\uD83D\uDE00'. The first code (high surrogate, D800-DBFF) and second code (low surrogate, DC00-DFFF) combine to represent the character. This tool handles surrogate pairs automatically.
Which characters are escaped vs kept as-is?
ASCII printable characters (space through ~, codes 32-126) are kept as-is for readability, except backslash which is always escaped. All other characters including newlines, tabs, Unicode letters, and emoji are converted to \uXXXX format. This balances readability with proper escaping.
Can I use this for JSON strings?
Yes! Unicode escape sequences are valid in JSON strings. This tool is perfect for creating JSON with international characters or emoji that need to work across all systems. JSON parsers automatically decode \uXXXX sequences back to the original characters.
What's the difference between \uXXXX and \xXX?
\uXXXX is Unicode escape with 4 hex digits, supporting characters up to U+FFFF (or surrogate pairs for higher). \xXX is byte escape with 2 hex digits, limited to 0-255. Unicode escaping is more versatile and works with all Unicode characters including international text and emoji.
Does this work with all programming languages?
Most modern languages support \uXXXX Unicode escapes including JavaScript, Java, C#, Python (with 'u' prefix), and JSON. Some languages use different formats like \U00XXXXXX (8 digits) or \x{XXXX}. Check your language's documentation for the exact syntax it supports.
Is my data sent to a server?
No, all escaping and unescaping happens in your browser using JavaScript. Your text never leaves your device, ensuring complete privacy and security. This is especially important when working with sensitive content or user data.