HTML Entity Encoder & Decoder

Stephanie Ben-Joseph headshot Stephanie Ben-Joseph

What this tool does

This page helps you convert between plain text and HTML entity references:

Use it when you need to safely display user-generated text in HTML, debug template output, or convert copied HTML source into readable characters. For security: encoding is one part of preventing cross-site scripting (XSS), but you must also apply the correct escaping for the context (HTML text vs attribute vs JavaScript vs URL).

How to use (quick steps)

  1. Paste or type text into the Input box.
  2. Select an action: Encode or Decode.
  3. Read the output in Result, then use Copy result.

Tip: If you paste already-encoded strings (for example <) and click Encode again, you may “double-encode” the ampersand (becoming <). That is expected—see Limitations & assumptions.

Why HTML entities matter

HTML uses certain characters to define structure:

If those characters appear in content that is meant to be displayed literally (for example showing a code snippet, or displaying a user’s message), they can break the document structure or introduce XSS risk. Entity encoding replaces those characters with safe sequences that the browser interprets as literal text.

Named vs numeric entities

HTML supports two main forms of entity references:

Numeric references map directly to Unicode code points. Conceptually, decoding a numeric entity converts an integer code point into a character.

Formulas (reference)

A numeric entity in decimal form uses a code point N:

&# N ;

A numeric entity in hexadecimal form uses the same code point written base-16:

&#x H ;

Where H is the hexadecimal representation of N. (For example, 169 decimal equals A9 hexadecimal.)

Comparison table

Type Example Pros Cons Typical use
Named entity && Readable, common for core reserved characters Not every Unicode character has a named entity Escaping HTML-reserved characters
Numeric (decimal) © → © Works for any Unicode code point Harder to read Interchange where named entity may be unknown
Numeric (hex) 😀 → 😀 Compact for some ranges; common in dev tools Harder to read than named entities Debugging, code snippets, documentation

Worked examples

Example 1: Encode for safe display in HTML

Input:

<script>alert("XSS")</script> & friends

Encoded output (what you can safely display as text in an HTML page):

&lt;script&gt;alert(&quot;XSS&quot;)&lt;/script&gt; &amp; friends

Interpretation: The browser will render the literal characters <script>... as text instead of executing them as markup.

Example 2: Decode HTML entities copied from source

Input:

Tom &amp; Jerry &copy; 1990

Decoded output:

Tom & Jerry © 1990

Example 3: Double-encoding behavior (common pitfall)

Input:

&lt;div&gt;

Encode output:

&amp;lt;div&amp;gt;

This is expected: the ampersand in &lt; is itself a reserved character, so it is encoded again.

Interpreting the result

FAQ

Should I use named or numeric entities?

For the core reserved characters (&, <, >, quotes), named entities are conventional and readable. Numeric entities are useful when a character doesn’t have a named entity or when you prefer a direct code-point form.

Does decoding turn &lt; into <?

Yes. Decoding converts valid entity references into their literal characters. If you have &amp;lt;, decoding once yields &lt;; decoding twice yields <.

Is this enough to prevent XSS?

It helps for HTML text-node context, but XSS prevention depends on context. For example, values placed inside JavaScript, CSS, or URLs require different escaping/encoding rules. Always use your framework’s recommended output-encoding functions for the exact context.

Will this convert every Unicode character into an entity?

No. Encoding here focuses on the characters that must be escaped for HTML structure. Most Unicode characters can be left as-is in UTF-8 pages. If you need “encode everything to numeric entities,” you’d use a different mode/tool.

Limitations & assumptions

HTML entity encoder and decoder

Paste plain text to encode, or paste entity text (e.g., &amp;, &lt;, &#169;) to decode. Newlines are preserved.

Enter text to encode or decode.

Embed this calculator

Copy and paste the HTML below to add the HTML Entity Encoder & Decoder (Named & Numeric) | AgentCalc to your website.