XML Character References: Decimal and Hex

XML character references are markup constructs used to represent individual Unicode characters through their unique numeric code points rather than their literal glyphs. This article explains what character references are, why they are used to handle special or reserved characters in XML documents, and how to properly format and apply both decimal and hexadecimal representations in your code.

What is an XML Character Reference?

A character reference is a way to include any legal Unicode character in an XML document by referencing its Unicode code point. They are particularly useful for: - Inserting characters that are difficult or impossible to type on a standard keyboard (such as foreign language glyphs, mathematical symbols, or typographic marks). - Preventing XML parser errors caused by characters that conflict with XML syntax (like < or &). - Ensuring document portability across systems that use different text encodings.

All numeric character references begin with an ampersand and a hash symbol (&#) and end with a semicolon (;).

Decimal Character References

Decimal character references represent a character’s Unicode code point in base-10 format.

Syntax

&#[decimal_value];

How to Use Decimal References

  1. Find the decimal Unicode code point for the target character.
  2. Place the decimal number directly after &# and close it with ;.

Examples

Hexadecimal Character References

Hexadecimal character references represent a character’s Unicode code point in base-16 format. This is often the preferred form because standard Unicode charts typically list code points in hexadecimal notation (e.g., U+00A9).

Syntax

&#x[hexadecimal_value];

Note: A lowercase x must precede the hexadecimal digits. The hexadecimal letters (AF or af) are case-insensitive.

How to Use Hexadecimal References

  1. Find the hexadecimal code point for the target character (omitting the U+ prefix).
  2. Place the hex value between &#x and ;.

Examples

Comparison: Decimal vs. Hexadecimal

Both decimal and hexadecimal forms achieve identical results during XML parsing. The choice between them comes down to context and convention:

Character Name Decimal Form Hexadecimal Form
< Less-than sign &#60; &#x3C;
> Greater-than sign &#62; &#x3E;
" Quotation mark &#34; &#x22;
Black Heart Suit &#9829; &#x2665;
ñ Latin Small Letter N with Tilde &#241; &#xF1;

While decimal forms use only standard numerals 0-9, hexadecimal forms require the x flag and can utilize digits 0-9 along with letters A-F.