Frage

Is there a good rule of thumb for when to use decimal vs. hexadecimal notation for HTML entities?

For example, a non-breaking hyphen is written in decimal as ‑ and in hex as ‑.

This answer says that hexadecimal is for Unicode; does that mean hex should be used if you're using the <meta charset="utf-8"> tag in the document <head>?

Occasionally, I will notice entity characters mistakenly rendered instead of the entities they represent -- for example, &amp; appearing (instead of an ampersand) in an email subject line or RSS headline. Is either hex or decimal better for avoiding this?

One last consideration: can using hex or decimal affect the rendering clarity (crispness) of the character?

War es hilfreich?

Lösung

The rule of thumb is: use whichever you prefer, but prefer hex. ☺

There is no difference in meaning and no difference in browser support (the last browsers that supported decimal references only died in the 1990s).

As @AlexW describes, hexadecimal references are more natural than decimal, due to the way character code standards are written. But if you find decimal references more convenient, use them.

The issue has nothing to with meta tags and character encodings. The main reason why character references were introduced into HTML is that they let you enter characters quite independently of the encoding of the document. This includes characters that cannot be directly written at all in the encoding used. Thanks to them, you can enter any Unicode character even if the character encoding is ASCII or some other limited encoding, like ISO-8859-1.

In the old days, it was common to recommend the use of named references (or “entity references” as they are formally called in classic HTML), when possible, because a reference like &Omega;, when displayed literally to the user, is more understandable than a reference like &#x3A9; or &#937;. This hasn’t been relevant for over a decade, as far as web browsers are considered. But e.g. e-mail clients might be kind of stupid^H^H^H^H^H^H^H^H^H underdeveloped in this respect. They might e.g. show references as such in a list of messages, even though they can intepret them properly when viewing a message. But there does not seem to be any consistent behavior that you could count on.

Andere Tipps

Overall

HTML (and XML) offers three ways to encode special characters: numeric hex &#x26;, numeric decimal &#38; (aka "character references"), and named &amp; (aka "entity references"). They've remained equally valid and fully supported by all major browsers for decades. They work with any encoding, but always render from the Unicode set (which is compatible with ASCII, ISO Latin, and Windows Latin, minus codes 128-159).

So it's up to personal preference, with a few things worth noting.

Necessity

If you add the proper charset meta tag to your HTML, you don't need to encode special characters at all (except & < > " ', or more generally, just & < in loose text). The exception is wanting to encode a character not present in the specified encoding. But if you use UTF-8, you can represent anything from Unicode anyway.

Brevity

For any character below index 10, decimal is shorter. A tab is &#9;, versus &#x09;, so it may be worth it for pre tags containing a lot of TSV data, for example.

Ease of Use

Named references are the easiest to use and memorize, especially for code shared among developers of different backgrounds and skill sets. &lt; is much more intuitive than &#x3c;. As for someone else's comment regarding relevance, they're actually still fully supported as part of the W3C standard, and have even been expanded on for HTML5.

Best Practice

Using named or decimal references may not be the best general practice since the names are English-only, and unique to HTML (even XML lacks named references, minus the "big five"). Most programming languages and character tables use hex encoding, so it makes things easier and more portable in the long run when you stay consistent. Though for small projects or special cases, it may not really matter.

More info: http://xmlnews.org/docs/xml-basics.html#references

These are called numeric character references. They are derived from SGML and the numeric portion of them references the specific Unicode code point of the character you are trying to display. They allow you to represent characters of Unicode, even if the particular character set you wrote the HTML in doesn't have the character you are referencing. Whether you reference the code point with decimal or hexidecimal does not matter, except for very old browsers that prefer decimal. Hexidecimal support was added because Unicode code points are referenced in hex notation and it makes it much easier to look up the code point and then add the reference, without having to convert to decimal:

U+007D

=

&#x007D;

To answer your question:

This answer says that hexadecimal is for Unicode; does that mean hex should be used if you're using the <meta charset="utf-8"> tag in the document ?

You have to understand that UTF-8 is backwards-compatible with ASCII / ISO-8859-1. So the first 256 characters of UTF-8 will be the same in ASCII and UTF-8. Hex is just easier for UTF-8 because, as of 2013 there are 1,114,112 Unicode code points. So it's easier to write &#x110000; than it is to write &#1114112; etc.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top