Unicode 文字をエンコードする方法
Unicode 文字を、エスケープシーケンス、コードポイント、UTF-8 バイトなどのさまざまな表現に変換します。
问题
非 ASCII の Unicode 文字(絵文字や CJK 文字など)を、ASCII のみを受け付けるシステムで表現する必要があります。
前提条件
- Unicode コードポイントの理解
- UTF-8 エンコーディングへの習熟
ステップ
-
1. Find the code point
Look up the Unicode code point of your character. For example, the euro sign € is U+20AC. The code point is a scalar value that uniquely identifies the character in the Unicode standard.
入力€输出U+20AC -
2. Convert to the target representation
For JavaScript strings, use \u20AC or \u{20AC}. For UTF-8 bytes, encode as 0xE2 0x82 0xAC. For HTML, use € or €. Choose the representation your target system expects.
入力€输出JS: \u20AC | UTF-8: E2 82 AC | HTML: € -
3. Verify round-trip fidelity
Decode the representation back to the original character and compare. Characters outside the Basic Multilingual Plane (above U+FFFF) require surrogate pairs in UTF-16, so pay extra attention to emoji and rare scripts.
入力\u20AC输出€
期待される結果
元の文字に正確にデコードし戻せる、ASCII 安全な Unicode 文字の表現。
よくあるエラー
- Using \uXXXX for characters above U+FFFF without surrogate pairs
- Use the \u{XXXXX} brace syntax in JavaScript or compute the surrogate pair manually for older environments.
- Confusing UTF-8 byte count with code point value
- UTF-8 uses 1-4 bytes per character. The byte values are not the same as the code point. Use a proper encoder.
トラブルシューティング指引
- If a character shows as two boxes, it was split into surrogate pairs incorrectly. Use a UTF-8-aware tool.
- Check that your file editor saves as UTF-8 without BOM to avoid hidden bytes.
セキュリティ上の制限
- Unicode encoding does not sanitize input. Be aware of homoglyph attacks where visually identical characters from different scripts are used to deceive.