エンコード
Unicode Escape
Unicode エスケープシーケンスのエンコード・デコード — 文字を \uXXXX 形式に、またはその逆に変換。
A Unicode escape sequence (\uXXXX) writes a character as its code point instead of the character itself — so a string literal in code can contain any Unicode character while the source file itself stays plain ASCII.
How to use it
- Encode: paste text and every non-ASCII character gets replaced with its matching \uXXXX sequence.
- Decode: paste a string containing \uXXXX sequences to get plain, readable text back.
- Characters outside the Basic Multilingual Plane (most emoji) are encoded as a surrogate pair of two sequences.
Common uses
- Reading JSON or JS code where non-ASCII text was pre-escaped into \uXXXX for compatibility.
- Inserting a character that's not on the keyboard into code via an explicit Unicode escape.
- Debugging why a string with an emoji shows up as two odd characters — recognizing a surrogate pair.
Things to keep in mind
\uXXXX encodes exactly one 16-bit UTF-16 unit, not one visible character — characters outside the Basic Multilingual Plane (most emoji) need two such sequences together (a surrogate pair).
JSON only requires escaping for control characters and a few special characters — escaping every character as \uXXXX isn't required, though it is valid.
よくある質問
Unicode エスケープシーケンスとは何ですか?
\uXXXXの形式で文字のコードポイントを16進数4桁で表す表記法です。JavaScriptやJSON、Javaの文字列リテラルなどで、非ASCII文字をソースコード上でASCIIだけを使って安全に表現するために使われます。
絵文字のような文字が \uXXXX 2つ分になるのはなぜですか?
絵文字などBMP(基本多言語面)外の文字はコードポイントが\uXXXX1つでは表せないため、サロゲートペアと呼ばれる2つの\uXXXXの組み合わせで表現されます。デコード時にはこのペアを正しく1文字として復元する必要があります。
「非 ASCII」と「すべての文字」の違いは何ですか?
「非ASCII」はASCII範囲(英数字や基本記号)を除いた文字だけをエスケープします。「すべての文字」はアルファベットや数字も含め、入力されたすべての文字を\uXXXX形式に変換します。
サロゲートペアを途中で分断するとどうなりますか?
対になるコードのない孤立したコード単位になってしまいます。ほとんどのパーサーは、このような文字列をエラーとして拒否するか、代替文字「�」として表示します。
絵文字1文字を\uXXXX1つだけでエンコードできますか?
いいえ。その絵文字が基本多言語面の外にある場合(現代の絵文字のほとんどがそうです)、常に2つの\uXXXXシーケンスから成るサロゲートペアが必要になります。