เว็บและ HTML

Punycode

การเข้ารหัสที่เข้ากันได้กับ ASCII สำหรับชื่อโดเมน Unicode แปลงป้ายกำกับที่ถูก internationalize เป็นสตริง ASCII ที่มีคำนำหน้า xn-- münchen.de → xn--mnchen-3ya.de

· Updated

What Is Punycode?

Punycode is an ASCII-compatible encoding algorithm for Unicode strings, defined in RFC 3492. It converts a Unicode string — potentially containing characters from any script — into a string using only ASCII letters, digits, and hyphens. Punycode is the mechanism that makes Internationalized Domain Names (IDNs) work within the ASCII-only DNS infrastructure.

A Punycode-encoded IDN label is prefixed with xn-- to mark it as an ACE (ASCII Compatible Encoding) label. The full domain münchen.de becomes xn--mnchen-3ya.de in Punycode.

The Algorithm

Punycode represents a Unicode string in two parts, separated by a hyphen:

  1. Basic code points (ASCII characters): copied verbatim before the final hyphen.
  2. Non-basic code points: encoded as delta values (variable-length integers) appended after the hyphen.
münchen → m-u-n-c-h-e-n (basic) + ü (non-basic: ü = U+00FC)
xn--mnchen-3ya
       ^      ^
       |      encoding of position and value of ü
       basic chars minus ü

The algorithm uses a generalized variable-length integer (generalized variable-length quantity, or "base-36 with bias") to encode the insertion positions and code point values compactly.

Examples

# Python standard library
"münchen".encode("punycode")         # b"mnchen-3ya"
"例え.jp".split(".")[0].encode("punycode")  # b"r8jz45g"

# With xn-- prefix (full IDNA encoding)
"münchen.de".encode("idna")          # b"xn--mnchen-3ya.de"
"例え.jp".encode("idna")             # b"xn--r8jz45g.jp"

# Decoding
b"mnchen-3ya".decode("punycode")     # "münchen"
b"xn--mnchen-3ya.de".decode("idna")  # "münchen.de"

Common Punycode Examples

Unicode Domain Punycode
münchen.de xn--mnchen-3ya.de
例え.jp xn--r8jz45g.jp
中文.com xn--fiq228c.com
한국어.한국 xn--bj0bj06e.xn--3e0b707e
مثال.إختبار xn--mgbh0fb.xn--kgbechtv
пример.испытание xn--e1afmapc.xn--80akhbyknj4f

Punycode in Browsers

Modern browsers display the Unicode form of IDN labels when the characters all come from a single script and the label passes homograph safety checks. Mixed-script domains or suspicious lookalike characters trigger display of the Punycode form as a security warning:

User types:    https://münchen.de/
Browser shows: https://münchen.de/     (safe: all Latin)
DNS query:     xn--mnchen-3ya.de

User types:    https://pаypal.com/     (Cyrillic а)
Browser shows: https://xn--pypal-4ve.com/  (suspicious: mixed script)

Punycode for Non-DNS Uses

Although designed for DNS, Punycode can encode any Unicode string. It is sometimes used in email systems (IDNA for the domain part) and in IRI (Internationalized Resource Identifier) processing. However, other encodings like percent-encoding are preferred for URL paths and query strings.

Limitations

  • Not encryption or compression: Punycode is purely a reversible encoding for ASCII transport.
  • Label length: Each DNS label encoded in Punycode must not exceed 63 ASCII characters.
  • Readability: xn--fiq228c.com is meaningless to humans — the whole point of IDN display in browsers is to hide this from users.

Quick Facts

Property Value
RFC RFC 3492
Purpose ASCII-compatible encoding for Unicode strings in DNS
Prefix for IDN labels xn--
Character set a–z, 0–9, -
Encoding style Basic ASCII copied; non-basic encoded as delta integers
Python codec "punycode" (bare) or "idna" (with xn-- prefix)
Max encoded label 63 ASCII characters

คำศัพท์ที่เกี่ยวข้อง

เพิ่มเติมใน เว็บและ HTML

CSS Text Direction

CSS properties (direction, writing-mode, unicode-bidi) controlling text layout direction. Works with Unicode …

HTML เอนทิตี

การแทนค่าอักขระในรูปแบบข้อความใน HTML สามรูปแบบ: ชื่อ (&), ทศนิยม (&), เลขฐานสิบหก (&) จำเป็นสำหรับอักขระที่ขัดแย้งกับไวยากรณ์ HTML

Internationalized Domain Name (IDN)

ชื่อโดเมนที่มีอักขระ Unicode ที่ไม่ใช่ ASCII เก็บไว้ภายในเป็น Punycode (xn--...) แต่แสดงเป็น Unicode ให้ผู้ใช้เห็น ความกังวลด้านความปลอดภัย: การโจมตีแบบ …

JavaScript Intl API

ECMAScript Internationalization API providing locale-aware string comparison (Collator), number formatting (NumberFormat), date …

Unicode in CSS

CSS supports Unicode via escape sequences (\2713 for ✓), the content property …

การอ้างอิงอักขระ XML

เวอร์ชัน XML ของการอ้างอิงอักขระเชิงตัวเลข: ✓ หรือ ✓ XML มีเพียง 5 entity ที่มีชื่อ (& …

การอ้างอิงอักขระที่มีชื่อ

HTML entity ที่ใช้ชื่อที่อ่านง่าย: © → ©, — → — HTML5 กำหนด 2,231 …

การอ้างอิงอักขระเชิงตัวเลข

HTML entity ที่ใช้หมายเลข code point Unicode: ทศนิยม (© → ©) หรือเลขฐานสิบหก (© …

การเข้ารหัสเปอร์เซ็นต์ (URL encoding)

การเข้ารหัสอักขระที่ไม่ใช่ ASCII และอักขระที่สงวนไว้ใน URL โดยแทนที่แต่ละไบต์ด้วย %XX ใช้ UTF-8 ก่อน แล้วเข้ารหัส percent แต่ละไบต์: …

การแสดงข้อความ

การเรนเดอร์อักขระด้วย glyph ข้อความสีเดียวธรรมดาแทนที่จะเป็น emoji แบบสี มักใช้ Variation Selector 15 (U+FE0E) เพื่อแทนที่การแสดงผล emoji …