# HTML entities: encoding modes, decoding and context

> Choose named, decimal or hexadecimal references and understand special-only encoding, one-pass decoding, Unicode limits and XML differences.

- Tool: [HTML Entity Encoder / Decoder](https://toolsg.com/en/html-entity-encoder)
- Language: en (available: en, zh, ms, ta)
- Last verified against official sources: 2026-10-19
- Source site: ToolSG — a planning guide, not official or financial advice. Cite the agencies listed under Sources.

## Key figures at a glance

| Fact | Value | Source |
| --- | --- | --- |
| Special-only mode | Five characters — Ampersand, less-than, greater-than, double quote and apostrophe. |  |
| Output forms | Named, decimal or hexadecimal — All-character named mode uses decimal references when the local name table has no match. |  |
| Decode behavior | One HTML parsing pass — The browser decodes references; it does not validate or sanitize the input. |  |
| Displayed lengths | UTF-16 code units — An emoji such as 🙂 counts as two units, not one displayed symbol. |  |

## Choose a mode and convert

Paste text, choose the reference type and select special-only or all-character mode, then press Encode. Decode uses the browser's HTML reference parser and does not depend on the encoding-mode radio buttons. Swap exchanges input and output; Copy copies the result.

Special-only mode transforms the five reserved characters listed above. All-character mode iterates Unicode code points. In named mode it uses a small built-in mapping, then decimal references for other characters; it does not search the full HTML named-reference table.

## Examples from the real page

| Input | Encoding choice | Result |
| --- | --- | --- |
| `<>&` | Special only, named | `&lt;&gt;&amp;` |
| `🙂` | All, decimal | `&#128578;` |
| `🙂` | All, hex | `&#x1F642;` |
| `©A` | All, named | `&copy;&#65;` |
| `A B` | All, named | `&#65;&#32;&#66;` |

An ordinary space becomes `&#32;` in all-character named mode. U+00A0, the non-breaking space, becomes `&nbsp;`. They are different characters. The encoder handles the emoji as one code point, while the length statistics use two UTF-16 units.

## Decoding and repeated encoding

Decoding `&amp;lt;` gives the literal reference text `&lt;`, not a less-than character in the same pass. Encoding an existing `&lt;` again produces `&amp;lt;`. Check which layer of encoding your target expects rather than repeatedly pressing the buttons.

Decode does not strip tags: input `<b>A</b>` remains that text in the output box. Invalid references follow the browser's HTML parser rules; this is not a strict validator. Some values do not round-trip, including the null reference, which becomes a replacement character.

## Context, XML and limits

HTML text, quoted attributes, URLs, JavaScript and CSS are different output contexts. This tool converts references; it does not select context-appropriate escaping or sanitize arbitrary markup. Use your framework's appropriate output mechanism for the destination.

HTML and XML have different entity sets. XML predefines only `amp`, `lt`, `gt`, `quot` and `apos`; names such as `nbsp` and `copy` need declarations. Numeric syntax alone does not make every value a valid XML character. Use XML-aware escaping and validation for XML documents.

Processing happens locally in the browser without uploading the text for conversion. Empty or whitespace-only input is rejected. The displayed reference count is a text-pattern count, not proof that every candidate is valid. Download writes an `.html` file; the extension does not establish that it is safe to render.

Use [Base64](https://toolsg.com/en/base64-encoder) for byte-to-text representation or [text to hex](https://toolsg.com/en/text-to-hex) to inspect UTF-8 bytes. Neither is interchangeable with HTML escaping.

## Frequently asked questions

**What are HTML entities?**

HTML entities are special codes that start with & and end with ; used to display reserved or special characters in HTML. For example, &lt; displays as < and &amp; displays as &. They prevent browsers from interpreting characters as HTML code.

**When should I use HTML entity encoding?**

Use entity encoding when a target HTML text context requires it. Attribute values, URLs, JavaScript and CSS have different rules; this tool does not choose the right context or sanitize markup.

**What's the difference between named and numeric entities?**

Named references identify characters by name; decimal and hexadecimal references use Unicode code-point numbers. Numeric syntax does not guarantee a valid or unchanged result for every value: HTML applies special rules to invalid and some legacy control values.

**Is my text data sent to any server?**

No, all HTML entity encoding and decoding happens entirely in your browser. Your data never leaves your device, making this tool completely safe for sensitive HTML code and content.

**Should I encode all characters or only special characters?**

Generally, encode only special characters (< > & " ') to keep file sizes small and content readable in the HTML source. Encoding all characters significantly increases file size and is only necessary in specific situations like preventing text injection attacks.

**Can I decode entities that are already in my HTML?**

Yes, the decode function converts HTML entities back to their original characters. This is useful for reading encoded content, editing, or converting between different encoding formats.

**What happens if I encode text twice?**

Double encoding encodes the entities themselves. For example, encoding &lt; again produces &amp;lt; which displays as &lt; in the browser instead of <. Always check if text is already encoded before encoding again.

**Which entity type should I use?**

Named entities are most readable and recommended for common characters. Use numeric entities for characters without named equivalents. Hexadecimal entities are preferred by developers who work with Unicode code points. All three types work correctly in modern browsers.

**Does HTML entity encoding prevent XSS attacks?**

Entity encoding is context-dependent output encoding, not an XSS sanitizer. It does not make arbitrary URLs, JavaScript, CSS or markup safe. Use the escaping or sanitization mechanism required by the destination context.

**Can I use this for XML documents?**

HTML and XML do not have identical entity sets. XML predefines only amp, lt, gt, quot and apos; names such as nbsp and copy need declarations. Use XML-aware escaping and validate the result, including XML character restrictions.

**What is the difference between HTML encoding and URL encoding?**

HTML encoding uses entities like &lt; for display in HTML documents. URL encoding uses percent-encoding like %3C for web addresses. They serve different purposes: HTML encoding for content display, URL encoding for transmitting data in URLs.

**Are there any characters that cannot be encoded?**

Not every Unicode value round-trips through HTML references. For example, the null reference becomes the replacement character, and invalid values have parser-defined handling. The tool is an HTML text converter, not a Unicode or XML validity checker.


## Sources

- [WHATWG — HTML named character references](https://html.spec.whatwg.org/multipage/named-characters.html)
- [WHATWG — Character reference parsing](https://html.spec.whatwg.org/multipage/parsing.html#character-reference-state)
- [W3C — XML predefined entities](https://www.w3.org/TR/xml/#sec-predefined-ent)
- [OWASP — Context-dependent output encoding](https://cheatsheetseries.owasp.org/cheatsheets/Cross_Site_Scripting_Prevention_Cheat_Sheet.html)
