Hexadecimal

From Fox Labs Wiki

Hexadecimal (also known as Base-16 or simply Hex) is a positional number system that represents numbers using a base of sixteen (16). Unlike the decimal system which uses ten (10) symbols, hexadecimal uses sixteen (16) distinct symbols. The most common representation appears in the numbers 0 through 9 and the characters A through F (representing ten through fifteen).

Software development relies heavily on the hexadecimal system, as it provides a convenient and efficient means for representing of binary-coded values. Each hexadecimal number represents 4 bits (binary digits), also known as a nibble (one half of a byte). For example, the values 0000 through to 1111 in binary, or 0 through 15 in decimal, can be represented in hexadecimal as 0 through F.

Representations

In order to distinguish hexadecimal numbers from that of regular decimal numbers, there are several different techniques employed:

  • Most commonly in C and similar languages, 0x is used as prefix to distinguish a hexadecimal number. For example: 0x0432.
  • In XML and XHTML, characters can be expressed in hexadecimal number when used in the &#x<value>; notation. For example: &#x2122; represents the Trademark symbol ™.
  • In assembly languages derived from Intel standards, hexadecimal is distinguished by the suffix H or h. For example: FFh or 0543H.
  • In Motorola derived assembly languages along with Delphi, Pascal and Commodore BASIC, the dollar sign is used as a prefix to distinguish hexadecimal values. For example: $1234.

In other uses, hexadecimal is always assumed to be the representation of the value, but have rules as well:

  • In URIs (including URLs), character codes are written as hexadecimal pairs with the % prefix. For example: %20 is the character code representation for the space character.
  • In the Unicode standard, a character code is represented the prefix U+ followed by the hexadecimal value. For example: U+0021 is the unicode representation of the exclamation point character (!).
  • Color references in HTML, CSS and X Window can be expressed in six hexadecimal digits (two each for red, green and blue respectively) prefixed with #. For example: #FFAA33 represents an orange colour: (Template:Color box). CSS also allows for a condensed three digit abbreviation of a colour (#FA3).
  • In MIME quoted-printable encoding, character codes are written as hexadecimal pairs prefixed with =. For example: Espa=F1a is "España".
  • MAC addresses are represented as six pairs of hexadecimal characters separated by a colon :. For example: 00:00:00:00:00:00.
  • IPv6 addresses are represented as eight groups of four hexadecimal characters separated by a colon :. For example: 2001:0db8:853a:0000:0000:8a2e:0370:7334. Additionally, IPv6 addresses can be abbreviated when a group contains all zeros (0000) by omitting the value: 2001:0db8:853a:::8a2e:0370:7334.
  • Universally unique identifiers (UUIDs) otherwise known as globally unique identifiers (GUIDs) are commonly expressed as thirty two (32) hexadecimal characters in groups of eight-four-four-four-twelve separated by a hyphen -. For example: 3F2504E0-4F89-41D3-9A0C-0305E82C3301.

Conversion

Binary conversion

Most computers operate using binary data which is difficult for humans to work with when dealing with even small numbers. Converting binary to hexadecimal makes it easier for converting to decimal and other formats easier.

For example, in order to convert the value 11112 to decimal:

  • 00012 = 110
  • 00102 = 210
  • 01002 = 410
  • 10002 = 810

Therefore:

11112 = 810 + 410 + 210 + 110
= 1510

As the numbers grow in size, it becomes less trivial to convert the value to decimal. For example, to convert the value 10010111002 to decimal:

10010111002 = 51210 + 6410 + 1610 + 810 + 410
= 60410

Compared to calculating the number into hexadecimal, where you can break the binary data up into distinct groups of 4 digits and convert them individually:

10010111002 = 00102 01012 11002
= 2 5 C
= 25C16

See also