HTML Entity Converter — Encode and Decode HTML Characters

An HTML entity converter replaces reserved characters with HTML character references, or converts those references back into readable characters. Encoding an ampersand as & prevents it from being confused with the start of a reference in HTML text. Decoding the same reference restores an ampersand. Choose named, decimal, or hexadecimal output, then inspect the exact characters before using them in markup, data exports, or documentation.

HTML Entity Converter

Preparing tool controls…

An HTML entity converter replaces reserved characters with HTML character references, or converts those references back into readable characters. Encoding an ampersand as & prevents it from being confused with the start of a reference in HTML text. Decoding the same reference restores an ampersand. Choose named, decimal, or hexadecimal output, then inspect the exact characters before using them in markup, data exports, or documentation.

Html Entity Encoder Decoder At a Glance

PropertyWhat to expect
Main operationsCharacter-reference encoding and decoding
Entity formatsNamed where available, decimal numeric, hexadecimal numeric
Character handlingReserved punctuation; optional non-ASCII characters
ScopeText transformation, not an HTML sanitizer
Maximum input500,000 UTF-16 code units

HTML Character References and Why They Matter

HTML has two distinct concerns: the text a person wants to display and the characters the HTML parser treats as syntax. A less-than sign can introduce a tag when inserted into HTML markup, while an ampersand may begin a named or numeric character reference. Writing these signs as references makes their intended meaning explicit within a text node. The five characters most commonly considered in encoding decisions are ampersand, less-than, greater-than, quotation mark, and apostrophe. Their treatment depends on whether the output is ordinary HTML text, a quoted attribute, JavaScript, CSS, or a completely different file format. Encoding is not a universal substitution that works identically in every context.

In a paragraph such as “5 < 7 & 8 > 6”, the punctuation is meant to be displayed rather than interpreted as markup. The encoded text can express it with references, so an HTML parser reads the intended comparison and conjunction. By contrast, when the source is an entire HTML document, encoding every angle bracket would turn real tags into displayed text and change the document’s behavior. Decide what you are converting before running the operation: visible page text, an attribute value, a source-code example, or an HTML file whose markup must remain intact. For editing layout markup itself, an HTML formatter treats tags as structure instead of converting them all into visible characters.

Choosing Named, Decimal, and Hexadecimal Entities

Named references such as &amp;, &lt;, and &copy; are often the easiest to recognize in a review. The named style in this converter uses common recognized names for punctuation and selected symbols, and falls back to numeric references when a preferred name is unavailable. That fallback matters for multilingual content: many characters have no short, familiar English-style name, but all valid Unicode scalar values can be expressed numerically. Choosing decimal output expresses an ampersand as &#38; in HTML source, while hexadecimal output expresses the same character as &#x26;. These three representations decode to the same visible glyph where supported by an HTML parser.

The optional non-ASCII switch governs how much of the input is represented by references. Leaving it off generally preserves accented letters, symbols, and other Unicode characters directly while encoding the reserved punctuation. Turning it on emits references for characters above ASCII, which can be useful when a receiving process expects an ASCII-only document. A Unicode character represented by a surrogate pair in JavaScript is handled as one code point during encoding, so an emoji does not become two misleading numeric references. Numeric output is reversible for valid scalar values, but switching representations does not correct text that was corrupted before it reached the converter.

How to Encode and Decode a Practical HTML Example

Begin with the literal phrase “Tom & Ana said: 3 < 5”. Choose Encode and leave quotation handling enabled. In named mode the ampersand is written as &amp; and the less-than sign as &lt;, while the ordinary letters and digits remain unchanged. The result should be pasted into the text portion of an HTML document rather than into a JavaScript string without further escaping. To check the round trip, copy the encoded result into the input, switch to Decode, and inspect the restored sentence. The returned text should match the initial phrase exactly, including spaces, curly punctuation, and line breaks where those were present.

To compare formats, encode the same expression once in decimal mode and again in hexadecimal mode. The displayed output differs because the numeric base differs, not because the character meanings change. When processing a collection of strings, treat each input as text rather than assembling a large HTML fragment and trying to infer which parts are tags. If you are debugging a query value containing ampersands, remember that URL percent encoding is a separate mechanism: &amp; is an HTML character reference, while %26 is an encoded URL component. Confusing them can lead to duplicated escaping and query strings that no longer represent their original values.

Decoding Rules, Double Encoding, and Error Cases

A decoder interprets recognized references but should not pretend every ampersand sequence is an entity. This converter recognizes numeric decimal and hexadecimal references, common named references, and additional standard browser-resolved names when the browser supplies that decoder. Ordinary strings such as “R&D” remain ordinary text rather than becoming speculative entities. Numeric code points outside Unicode, surrogate values, and the null code point are replaced with the Unicode replacement character and counted as invalid in the result. This is preferable to silently constructing invalid characters, but it also indicates that the original reference needs correction at its source.

Double encoding is a frequent cause of confusing output. If a source already contains &amp;, encoding its ampersand again produces &amp;amp;; one decoding pass then leaves the inner reference rather than the final visible symbol. The tool intentionally performs one transformation pass and shows a literal preview, which makes such cases inspectable. Optional decoding without semicolons is provided for messy snippets, but omission makes boundaries ambiguous and can misread adjoining letters or numbers. For reliable generation of new HTML, include semicolons and avoid repeatedly encoding the same material across templates, content management systems, and API layers.

Security Boundaries and Where Encoding Belongs

Escaping HTML syntax is useful for displaying trusted or untrusted text, but it does not amount to complete sanitization. Encoding text before inserting it into an HTML text node is different from assigning untrusted input to an event-handler attribute, a script element, a CSS declaration, or a URL-valued attribute. Each context has its own grammar, and an entity converter cannot determine whether a dangerous link scheme, malformed document, or executable code was introduced elsewhere. When a web application needs to display user content, DOM APIs that assign text content are generally safer than concatenating raw strings into HTML and trying to repair the result afterward.

The transformation does not contact a remote webpage, inspect a site, or validate an entire HTML document. It is suitable for comparing encodings, repairing copied examples, understanding character references, and preparing literal text for a known destination. Before publishing source excerpts, confirm how your editor treats escape sequences and whether a templating engine already escapes output. If another application expects data in JSON, a JSON syntax check can identify structural mistakes after the text has been placed in its actual JSON representation, rather than assuming that HTML encoding is meaningful inside every file format.

Frequently Asked Questions

What does an HTML entity converter actually change?

It changes the representation of characters in text. The encoded form contains ampersand-led references, and the decoded form contains the corresponding Unicode characters. The converter does not transform a webpage design, crawl a URL, or change the semantics of a properly parsed character reference.

Should I encode every non-ASCII character?

Usually not. HTML documents served as UTF-8 can include ordinary Unicode characters directly. ASCII-only output may be useful for legacy integrations, testing, or a constrained export pipeline, but numeric references make text less readable during manual editing.

Why does &amp; sometimes appear on a live page?

The content may have been encoded more than once, or the application may be treating already escaped HTML as literal text. Inspect each processing layer before decoding blindly. A fix often belongs where the string is inserted, rather than in the original content.

Can I use these entities inside JavaScript or JSON?

The representations are designed for HTML parsing contexts. JSON and JavaScript have different string-escaping rules, so an HTML reference may remain a literal string instead of becoming a character. Choose the encoding mechanism for the destination format.

Is HTML entity encoding enough to prevent cross-site scripting?

No. It can safely represent certain characters in HTML text contexts, but it does not sanitize every URL, attribute, script, style or DOM insertion. Use context-aware escaping, safe DOM insertion, and an appropriate sanitizer when accepting HTML markup.

Why might an unknown named reference remain unchanged?

A name may be misspelled, unsupported by the decoder, or written without a required semicolon. The converter leaves references it does not confidently recognize rather than inventing a replacement character. Numeric references are often the least ambiguous alternative.

Conclusion

HTML entities are most dependable when their destination context is known. Encode reserved text characters before inserting literal text into HTML, decode references one pass at a time, and compare the result against the source before publishing. A visibly correct conversion is only one part of safe markup handling; application-level escaping and HTML sanitization still need separate attention.