sodcalculator

10 Tech How-To

Unicode to HTML Entities: When & How to Convert

Guide 10-58922 · Updated · Step by step

Converting Unicode text to HTML entities replaces special characters (like ©, é, or 😀) with their numeric or named equivalents (©, é, 😀). This guide explains when you need to do this and provides a step-by-step method for manual conversion or using online tools, plus common pitfalls and troubleshooting.

What You’ll Need

  • The Unicode text you want to convert (copy it to your clipboard).
  • A text editor (Notepad, VS Code, or any code editor) to paste the result.
  • Optional: an online converter tool (see Step 2) or a programming environment like Python, JavaScript, or PHP.

Step-by-Step

  1. Identify the characters that need conversion. ASCII characters (letters, digits, basic punctuation) are already valid HTML and don’t need conversion. Only characters outside the ASCII range (code points > 127) and a few special reserved characters (<, >, &) should be converted. Typical examples: © (U+00A9), é (U+00E9), € (U+20AC), emoji (U+1F600).
  2. Choose your conversion method. You have three main options:
    • Manual conversion – for a few characters, look up the Unicode code point and write the numeric entity yourself.
    • Online converter – paste your text into a tool like W3Schools or FreeFormatter and copy the output.
    • Programming – use a function in your language of choice (e.g., htmlentities() in PHP, escape() in JavaScript, or a library in Python).
  3. Convert manually (if only a few characters):
    1. Find the Unicode code point of the character. For example, the copyright symbol © is U+00A9 (hex) or 169 (decimal).
    2. Write the numeric character reference as © (decimal) or © (hex). The decimal form is more common and safer.
    3. Replace the character in your text with this entity. For example, “© 2024” becomes “© 2024”.
  4. Convert using an online tool:
    1. Open your preferred converter (search “unicode to html entities”).
    2. Paste your text into the input box.
    3. Click the “Convert” or “Encode” button.
    4. Copy the output. It will typically show both decimal and hex versions; choose the decimal one unless you have a reason to use hex.
  5. Convert using JavaScript (in the browser): If you have a string in JavaScript, you can use escape() but it’s deprecated. A better approach is to loop through characters and replace those with code points > 127:
    function toHtmlEntities(str) {
      return str.replace(/[^\x00-\x7F]/g, function(ch) {
        return "&#" + ch.charCodeAt(0) + ";";
      });
    }

    This replaces every non-ASCII character with its decimal entity.

  6. Convert using Python: Use the html module:
    import html
    html.escape(text, quote=False)

    This converts <, >, & and also non-ASCII characters if you pass quote=True? Actually, html.escape() only escapes &, <, >, and quotes. To convert all non-ASCII to entities, you need a custom function:

    def to_entities(s):
        return ''.join('&#{};'.format(ord(c)) if ord(c) > 127 else c for c in s)
  7. Convert using PHP: Use htmlentities($text, ENT_QUOTES, 'UTF-8'). This converts all applicable characters to HTML entities.
  8. Verify the output: Paste the converted text into an HTML file and open it in a browser. The entities should render as the original characters. If you see literal “©” instead of ©, then your entity is malformed (e.g., missing semicolon).

Common Mistakes

  • Converting ASCII characters unnecessarily: Converting letters like “A” to “A” works but bloats the HTML and makes it unreadable. Only convert characters above U+007F.
  • Using the wrong base: Decimal entities use ©, hex use ©. Mixing them up (e.g., writing © as decimal) will display the wrong character.
  • Forgetting the semicolon: The semicolon is mandatory. © without the semicolon may not render correctly in some browsers.
  • Not encoding the ampersand itself: If your text contains a literal “&” (e.g., “AT&T”), it must be written as & to avoid being interpreted as the start of an entity.
  • Assuming all Unicode characters have named entities: Named entities (like ©) exist for only a few hundred characters. For most, you must use numeric entities.

Tips

  • Always use UTF-8 as your document’s character encoding (<meta charset="UTF-8">). When the encoding is UTF-8, you usually don’t need to convert Unicode text to entities at all – you can just write the characters directly. Entities are only necessary for older systems or when you need to embed text in an attribute that might get parsed.
  • For dynamic content (e.g., user input), always convert to entities to prevent XSS attacks. This is a security best practice.
  • If you’re generating HTML programmatically, use your language’s built-in escaping function rather than writing your own – it’s less error-prone.
  • For emoji, note that some are represented as a sequence of two code points (surrogate pair). A simple per-character conversion will produce two entities, which is correct.

Troubleshooting

  • The entity shows as literal text (e.g., “©” appears on the page): This usually means the semicolon is missing or the entity is inside a code block or pre tag that escapes HTML. Check that you wrote &#169; if you want to display the entity itself.
  • The character displays as a question mark or box: This means the browser can’t render the character. It could be that the font doesn’t support it, or the document encoding is not UTF-8. Ensure your HTML file is saved as UTF-8 and that the meta charset is set.
  • The conversion tool produces hex entities, but you need decimal: Most tools offer both options. If not, convert hex to decimal manually: © is 169 in decimal, so ©.
  • Double encoding: If you run the conversion twice, you get &#169; instead of ©. This happens when you apply the conversion to already-encoded text. Avoid converting multiple times.
  • PHP’s htmlentities() returns empty string for some characters: This can happen if the input is not valid UTF-8. Use mb_convert_encoding() to sanitize the input first.

Frequently Asked Questions

When do I actually need to convert Unicode to HTML entities?

You need to convert when your HTML document uses a character encoding that doesn’t support Unicode (like ISO-8859-1) or when you’re embedding user-generated content and want to prevent XSS attacks. In modern websites with UTF-8, you can often skip conversion entirely.

What’s the difference between numeric and named entities?

Numeric entities (e.g., ©) work for any character and use the Unicode code point. Named entities (e.g., ©) are easier to read but only exist for a limited set of characters.

Should I use decimal or hex entities?

Both are valid. Decimal is more common and easier to type. Hex is sometimes preferred because it aligns with Unicode documentation. Choose one and be consistent.

Do I need to convert spaces and newlines?

No. Spaces and newlines are ASCII characters and don’t need conversion. However, if you want to preserve multiple spaces in HTML, use   for non-breaking spaces.

Can I convert emoji to HTML entities?

Yes. Emoji have code points above U+FFFF, so they are represented as two surrogate code units in JavaScript. A standard per-character conversion will produce two entities, which is correct and will render the emoji.

Is it safe to use htmlentities() in PHP for all user input?

Yes, it’s a good practice. Just be sure to set the character encoding parameter to ‘UTF-8’ and use ENT_QUOTES to escape both single and double quotes. This prevents XSS attacks.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →