# HTML Entity Encoder and Decoder

URL: https://hifreetools.com/developer/html-entity-encoder

> Encode reserved and non-ASCII characters into safe HTML entities, or decode named, decimal, and hexadecimal entities back into the original text. Everything runs locally in your browser, so nothing you paste ever leaves your device.

## Quick answer

To encode, paste your text and the tool replaces the reserved characters & < > " ' with their named entities (and optionally every non-ASCII character with a numeric entity like &#233;). To decode, paste HTML containing entities and the tool looks each one up in a table of common named entities or parses numeric &#NN; and hexadecimal &#xNN; forms to rebuild the plain text. Choose the Encode or Decode mode and the result updates live as you type.

## Formula

- Encode reserved characters: & -> &amp; , < -> &lt; , > -> &gt; , " -> &quot; , ' -> &#39;
- Encode non-ASCII (optional): any character with code point > 127 -> &#<code point>;
- Decode any reference: named: &name; -> table[name] ; decimal: &#N; -> char(N) ; hex: &#xH; -> char(parseInt(H,16))

HTML reserves a handful of characters that change meaning inside markup: the ampersand (&), less-than (<), greater-than (>), double-quote ("), and apostrophe ('). When you want these characters to display literally instead of being parsed as tags or attribute boundaries, you replace them with character references called entities. In Encode mode this tool walks the input one Unicode code point at a time, swapping each reserved character for its named entity (& becomes &amp;, < becomes &lt;, > becomes &gt;, " becomes &quot;, ' becomes &#39;). When the optional "encode all non-ASCII" setting is on, any character above code point 127 (accented letters, dashes, currency symbols, emoji) is also converted to a numeric decimal entity of the form &#N; so the output is pure ASCII and safe to embed anywhere regardless of page encoding. In Decode mode the tool scans for character references with a single pass: named references (like &copy; or &lt;) are resolved through a built-in table of common named entities, decimal references (&#8364;) are parsed as base-10 code points, and hexadecimal references (&#x20AC; or &#X20AC;) are parsed as base-16, then rebuilt with String.fromCodePoint. Unknown or malformed references are left untouched so no data is silently lost. All processing is synchronous JavaScript that runs entirely in your browser.

## Examples

- Encode an HTML snippet: <script>alert("Hi & bye")</script> → &lt;script&gt;alert(&quot;Hi &amp; bye&quot;)&lt;/script&gt;
- Decode named entities: Tom &amp; Jerry &lt;3 → Tom & Jerry <3
- Encode non-ASCII to numeric entities: Café — €5 → Caf&#233; &#8212; &#8364;5
- Decode hexadecimal entities: &#x3C;b&#x3E;hi&#x3C;/b&#x3E; → <b>hi</b>

## FAQ

**What is an HTML entity?**
An HTML entity (character reference) is a code that represents a character that is reserved or hard to type directly. It starts with an ampersand and ends with a semicolon, for example &amp; for &, &lt; for <, or numeric forms like &#233; and &#x20AC;. The browser renders the entity as the character it stands for.

**Which characters must I encode in HTML?**
At minimum the five HTML-significant characters: & (&amp;), < (&lt;), > (&gt;), " (&quot;), and ' (&#39;). The ampersand and angle brackets matter in element text, while the quotes matter inside attribute values. Encoding all non-ASCII characters is optional but makes the output safe regardless of the page's character encoding.

**What is the difference between named and numeric entities?**
Named entities use a memorable label, like &copy; or &euro;, and only exist for a defined set of characters. Numeric entities reference a character by its Unicode code point in decimal (&#8364;) or hexadecimal (&#x20AC;) and work for any character. This decoder understands all three forms; the encoder emits named entities for the five reserved characters and decimal numeric entities for non-ASCII.

**Does decoding handle hexadecimal entities like &#x20AC;?**
Yes. The decoder recognizes named references through a lookup table, decimal references such as &#8364;, and hexadecimal references such as &#x20AC; or &#X20AC; (the x is case-insensitive). Each is converted to the matching Unicode character with String.fromCodePoint.

**Why does my apostrophe become &#39; instead of &apos;?**
&apos; is valid in XHTML and HTML5 but was never part of HTML4, so some older parsers do not recognize it. To stay maximally compatible this tool encodes the single quote as the numeric &#39;, which every browser decodes correctly.

**Is my text uploaded anywhere when I encode or decode?**
No. All encoding and decoding happens entirely in your browser with plain JavaScript. Nothing you paste is sent to a server, logged, or stored, so it is safe to use with private or internal content.

**What happens to an entity the decoder does not recognize?**
Unknown or malformed references are left exactly as they are rather than being deleted. For example a stray &fakeentity; or a malformed &#; is passed through unchanged, so you never lose data and can spot the problem in the output.


## Sources

- [MDN Web Docs - HTML character references](https://developer.mozilla.org/en-US/docs/Glossary/Character_reference)
- [WHATWG HTML Standard - Named character references](https://html.spec.whatwg.org/multipage/named-characters.html)
- [MDN - String.fromCodePoint()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCodePoint)

Runs in the browser; nothing is uploaded. Cite as: HIFreeTools, "HTML Entity Encoder and Decoder", https://hifreetools.com/developer/html-entity-encoder
