kexxcream
kexxcream

Reputation: 5943

Why HTML decimal and HTML hex?

I have tried to Google quite a while now for an answer why HTML entities can be compiled either in HTML decimal or HTML hex. So my questions are:

Upvotes: 8

Views: 4278

Answers (2)

Jukka K. Korpela
Jukka K. Korpela

Reputation: 201896

Originally, HTML was nominally based on SGML, which has decimal character references only. Later, the hexadecimal alternative was added in HTML 4.01 (and soon implemented in browsers), then retrofitted into SGML in the Web Adaptations Annex.

The apparent main reason for adding the hexadecimal alternative was that all modern character code and encoding standards, such as Unicode, use hexadecimal notation for the code numbers of characters. The ability to refer to a character by its Unicode number, written in the conventional hexadecimal notation, just prefixed with &#x and suffixed with ;, helps to avoid errors that may arise if people convert from hexadecimal to decimal notation.

Upvotes: 9

Codie CodeMonkey
Codie CodeMonkey

Reputation: 7946

There are three radixes used in computer technologies:

  • Binary, radix 2, because ultimately integers are arrays of switches, each which may be on (1) or off (0).
  • Octal, radix 8, because each digit represents exactly 3 bits, so it's easy to convert to binary.
  • Decimal, radix 10, because humans have 10 fingers and because we grew up using this radix.
  • Hexadecimal, radix 16, because like octal it's easy to convert to bits, but even better because 2 hex digits can be expressed in exactly 1 byte. If, for example, you see an rgba value given in hex as 0x00ff00ff, you can see instantly that it represents opaque green.

So, to answer the question posed, for some of us hex is the natural way to express integers as it gives more insight into the storage. For others it's decimal. To each his or her own!

Finishing with an HTML example: could &65536; be a character of utf-16? In hex it's easy to see that the answer is no, because its the same as &x10000; which needs more than 16 bits.

Upvotes: 0

Related Questions