Lably
Skip to content

Hexadecimal

Base-16 numeric representation

  • 20 min read
  • 7 chapters
  • 1 lab on Lably

Learn it on Lably

As typical computer hardware is binary in nature and that hex is power of 2, the hex representation is often used in computing as a dense representation of binary information. A hex digit represents 4 contiguous bits –known as a nibble. An 8-bit byte is two hex digits, such as 2C.

Special notation is often used to indicate that a number is hex. In mathematics, a subscript is typically used to specify the base. For example, the decimal value 491 would be expressed in hex as 1EB16. In computer programming, various notations are used. In C and many related languages, the prefix 0x is used. For example, 0x1EB.

Written representation

Common convention

Typically, a hex representation convention allows either lower or upper case letters and treats the letter the same regardless of its case.

Often when rendering non-textual data, a value stored in memory is displayed as a sequence of hex digits with spaces between values. For instance, in the following hex dump, each 8-bit byte is a 2-digit hex number, with spaces between them, while the 32-bit offset at the start is an 8-digit hex number.

<span id="mwMg"></span><span class="nl" id="mwMw">00000000</span><span class="w" id="mwNA"> </span><span class="mh" id="mwNQ">57</span><span class="w" id="mwNg"> </span><span class="mh" id="mwNw">69</span><span class="w" id="mwOA"> </span><span class="mh" id="mwOQ">6B</span><span class="w" id="mwOg"> </span><span class="mh" id="mwOw">69</span><span class="w" id="mwPA"> </span><span class="mh" id="mwPQ">70</span><span class="w" id="mwPg"> </span><span class="mh" id="mwPw">65</span><span class="w" id="mwQA"> </span><span class="mh" id="mwQQ">64</span><span class="w" id="mwQg"> </span><span class="mh" id="mwQw">69</span><span class="w" id="mwRA"> </span><span class="mh" id="mwRQ">61</span><span class="w" id="mwRg"> </span><span class="mh" id="mwRw">2C</span><span class="w" id="mwSA"> </span><span class="mh" id="mwSQ">20</span><span class="w" id="mwSg"> </span><span class="mh" id="mwSw">74</span><span class="w" id="mwTA"> </span><span class="mh" id="mwTQ">68</span><span class="w" id="mwTg"> </span><span class="mh" id="mwTw">65</span><span class="w" id="mwUA"> </span><span class="mh" id="mwUQ">20</span><span class="w" id="mwUg"> </span><span class="mh" id="mwUw">66</span> <span class="nl" id="mwVA">00000010</span><span class="w" id="mwVQ"> </span><span class="mh" id="mwVg">72</span><span class="w" id="mwVw"> </span><span class="mh" id="mwWA">65</span><span class="w" id="mwWQ"> </span><span class="mh" id="mwWg">65</span><span class="w" id="mwWw"> </span><span class="mh" id="mwXA">20</span><span class="w" id="mwXQ"> </span><span class="mh" id="mwXg">65</span><span class="w" id="mwXw"> </span><span class="mh" id="mwYA">6E</span><span class="w" id="mwYQ"> </span><span class="mh" id="mwYg">63</span><span class="w" id="mwYw"> </span><span class="mh" id="mwZA">79</span><span class="w" id="mwZQ"> </span><span class="mh" id="mwZg">63</span><span class="w" id="mwZw"> </span><span class="mh" id="mwaA">6C</span><span class="w" id="mwaQ"> </span><span class="mh" id="mwag">6F</span><span class="w" id="mwaw"> </span><span class="mh" id="mwbA">70</span><span class="w" id="mwbQ"> </span><span class="mh" id="mwbg">65</span><span class="w" id="mwbw"> </span><span class="mh" id="mwcA">64</span><span class="w" id="mwcQ"> </span><span class="mh" id="mwcg">69</span><span class="w" id="mwcw"> </span><span class="mh" id="mwdA">61</span> <span class="nl" id="mwdQ">00000020</span><span class="w" id="mwdg"> </span><span class="mh" id="mwdw">20</span><span class="w" id="mweA"> </span><span class="mh" id="mweQ">74</span><span class="w" id="mweg"> </span><span class="mh" id="mwew">68</span><span class="w" id="mwfA"> </span><span class="mh" id="mwfQ">61</span><span class="w" id="mwfg"> </span><span class="mh" id="mwfw">74</span><span class="w" id="mwgA"> </span><span class="mh" id="mwgQ">20</span><span class="w" id="mwgg"> </span><span class="mh" id="mwgw">61</span><span class="w" id="mwhA"> </span><span class="mh" id="mwhQ">6E</span><span class="w" id="mwhg"> </span><span class="mh" id="mwhw">79</span><span class="w" id="mwiA"> </span><span class="mh" id="mwiQ">6F</span><span class="w" id="mwig"> </span><span class="mh" id="mwiw">6E</span><span class="w" id="mwjA"> </span><span class="mh" id="mwjQ">65</span><span class="w" id="mwjg"> </span><span class="mh" id="mwjw">20</span><span class="w" id="mwkA"> </span><span class="mh" id="mwkQ">63</span><span class="w" id="mwkg"> </span><span class="mh" id="mwkw">61</span><span class="w" id="mwlA"> </span><span class="mh" id="mwlQ">6E</span> <span class="nl" id="mwlg">00000030</span><span class="w" id="mwlw"> </span><span class="mh" id="mwmA">20</span><span class="w" id="mwmQ"> </span><span class="mh" id="mwmg">65</span><span class="w" id="mwmw"> </span><span class="mh" id="mwnA">64</span><span class="w" id="mwnQ"> </span><span class="mh" id="mwng">69</span><span class="w" id="mwnw"> </span><span class="mh" id="mwoA">74</span><span class="w" id="mwoQ"> </span><span class="mh" id="mwog">2C</span><span class="w" id="mwow"> </span><span class="mh" id="mwpA">20</span><span class="w" id="mwpQ"> </span><span class="mh" id="mwpg">69</span><span class="w" id="mwpw"> </span><span class="mh" id="mwqA">6E</span><span class="w" id="mwqQ"> </span><span class="mh" id="mwqg">63</span><span class="w" id="mwqw"> </span><span class="mh" id="mwrA">6C</span><span class="w" id="mwrQ"> </span><span class="mh" id="mwrg">75</span><span class="w" id="mwrw"> </span><span class="mh" id="mwsA">64</span><span class="w" id="mwsQ"> </span><span class="mh" id="mwsg">69</span><span class="w" id="mwsw"> </span><span class="mh" id="mwtA">6E</span><span class="w" id="mwtQ"> </span><span class="mh" id="mwtg">67</span> <span class="nl" id="mwtw">00000040</span><span class="w" id="mwuA"> </span><span class="mh" id="mwuQ">20</span><span class="w" id="mwug"> </span><span class="mh" id="mwuw">79</span><span class="w" id="mwvA"> </span><span class="mh" id="mwvQ">6F</span><span class="w" id="mwvg"> </span><span class="mh" id="mwvw">75</span><span class="w" id="mwwA"> </span><span class="mh" id="mwwQ">20</span><span class="w" id="mwwg"> </span><span class="mh" id="mwww">28</span><span class="w" id="mwxA"> </span><span class="mh" id="mwxQ">61</span><span class="w" id="mwxg"> </span><span class="mh" id="mwxw">6E</span><span class="w" id="mwyA"> </span><span class="mh" id="mwyQ">64</span><span class="w" id="mwyg"> </span><span class="mh" id="mwyw">20</span><span class="w" id="mwzA"> </span><span class="mh" id="mwzQ">6D</span><span class="w" id="mwzg"> </span><span class="mh" id="mwzw">65</span><span class="w" id="mw0A"> </span><span class="mh" id="mw0Q">29</span><span class="w" id="mw0g"> </span><span class="mh" id="mw0w">21</span>

Identification

There are several conventions for expressing that a number is represented as hex.

  • A decimal subscript can give the base explicitly. For example 15910 indicates decimal 159, 15916 indicates hex 159. Some prefer a text subscript, such as 159decimal and 159hex, or 159d and 159h
  • In C and many languages influenced by it, the prefix 0x indicates that the numeric literal after it is in hex, a character of a string or character literal can be expressed as hex with the prefix \x (for example '\x1B' represents the Esc control character) and to output an integer as hex via printf-like function, the format conversion code %X or %x is used
  • In URIs (including URLs), character codes are written as hex pairs prefixed with %: http://www.example.com/name%20with%20spaces where %20 is the code for the space (blank) character, ASCII code point 20 in hex, 32 in decimal.
  • In XML and XHTML, a character can be expressed as a hex numeric character reference using the notation &#xcode;, for instance &#x0054; represents the character U+0054 (the uppercase letter "T"). If there is no x the number is decimal (thus &#0084; is the same character).
  • In Intel-derived assembly languages and Modula-2, hex is denoted with a suffixed H or h: FFh or 05A3H. Some implementations require a leading zero when the first hex digit character is not a decimal digit, so one would write 0FFh instead of FFh. Some other implementations (such as NASM) allow C-style numbers (0x42)
  • Some assembly languages (Microchip) use the notation H'ABCD' (for ABCD16); similarly, Fortran 95 uses Z'ABCD'
  • Ada and VHDL enclose hex numerals in based "numeric quotes": 16#5A3#, 16#C1F27ED#. For bit vector constants VHDL uses the notation x"5A3", x"C1F27ED".
  • Verilog represents hex constants in the form 8'hFF, where 8 is the number of bits in the value and FF is the hex constant
  • PostScript and the Bourne shell and its derivatives denote hex with prefix 16#: 16#5A3, 16#C1F27ED
  • Common Lisp uses the prefixes #x and #16r. Setting the variables *read-base* and *print-base* to 16 can also be used to switch the reader and printer of a Common Lisp system to hex representation for reading and printing numbers. Thus hex numbers can be represented without the #x or #16r prefix code, when the input or output base has been changed to 16.
  • MSX BASIC, QuickBASIC, FreeBASIC and Visual Basic prefix hex numbers with &H: &H5A3
  • TI-89 and 92 series uses a 0h prefix: 0h5A3, 0hC1F27ED
  • ALGOL 68 uses the prefix 16r to denote hex numbers: 16r5a3, 16rC1F27ED. Binary, quaternary (base-4), and octal numbers can be specified similarly.
  • The most common format for hex on IBM mainframes (zSeries) and midrange computers (IBM i) running the traditional OS's (zOS, zVSE, zVM, TPF, IBM i) is X'5A3' or X'C1F27ED', and is used in Assembler, PL/I, COBOL, JCL, scripts, commands and other places. This format was common on other (and now obsolete) IBM systems as well. Occasionally quotation marks were used instead of apostrophes.
  • Donald Knuth used a typewriter typeface to represent hex in his book The TeXbook, like: 5A3, C1F27ED

Implicit

In some contexts, a number is always written as hex, and therefore, needs no identification notation.

  • In the Unicode standard, a character value is represented with U+ followed by the hex value, e.g. U+00A1 is the inverted exclamation point (¡).
  • Color references in HTML, CSS and X Window can be expressed with six hex digits (two each for the red, green and blue components, in that order) prefixed with #: magenta, for example, is represented as #FF00FF. CSS also allows 3-hexdigit abbreviations with one hexdigit per component: #FA3 abbreviates #FFAA33, a golden orange.
  • In MIME (e-mail extensions) quoted-printable encoding, character codes are written as hex pairs prefixed with =: Espa=F1a is "España" (F1hex is the code for ñ in the ISO/IEC 8859-1 character set).)
  • PostScript binary data (such as image pixels) can be expressed as unprefixed consecutive hex pairs: AA213FD51B3801043FBC ...
  • Any IPv6 address can be written as eight groups of four hex digits (sometimes called hextets), where each group is separated by a colon (:). This, for example, is a valid IPv6 address: 2001:0db8:85a3:0000:0000:8a2e:0370:7334 or abbreviated by removing leading zeros as 2001:db8:85a3::8a2e:370:7334 (IPv4 addresses are usually written in decimal).
  • Globally unique identifiers are written as thirty-two hex digits, often in unequal hyphen-separated groupings, for example 3F2504E0-4F89-41D3-9A0C-0305E82C3301.

Alternative symbols

Notable other hexadecimal representations that use symbols other than letters "A" through "F" to represent the digits above 9 include:

  • During the 1950s, some installations, such as Bendix-14, favored using the digits 0 through 5 with an overline to denote the values 10–15 as 0, 1, 2, 3, 4 and 5.
  • The SWAC (1950) and Bendix G-15 (1956) computers used the lowercase letters u, v, w, x, y and z for the values 10 to 15.
  • The ORDVAC and ILLIAC I (1952) computers (and some derived designs, e.g. BRLESC) used the uppercase letters K, S, N, J, F and L for the values 10 to 15.
  • The Librascope LGP-30 (1956) used the letters F, G, J, K, Q and W for the values 10 to 15.
  • On the PERM (1956) computer, hex numbers were written as letters O for zero, A to N and P for 1 to 15. Many machine instructions had mnemonic hex-codes (A=add, M=multiply, L=load, F=fixed-point etc.); programs were written without instruction names.
  • The Honeywell Datamatic D-1000 (1957) used the lowercase letters b, c, d, e, f, and g whereas the Elbit 100 (1967) used the uppercase letters B, C, D, E, F and G for the values 10 to 15.
  • The Monrobot XI (1960) used the letters S, T, U, V, W and X for the values 10 to 15.
  • The NEC parametron computer NEAC 1103 (1960) used the letters D, G, H, J, K (and possibly V) for values 10–15.
  • The Pacific Data Systems 1020 (1964) used the letters L, C, A, S, M and D for the values 10 to 15.
  • New numeric symbols and names were introduced in the Bibi-binary notation by Boby Lapointe in 1968.
  • Bruce Alan Martin of Brookhaven National Laboratory considered the choice of A–F "ridiculous". In a 1968 letter to the editor of the CACM, he proposed an entirely new set of symbols based on the bit locations.
  • In 1972, Ronald O. Whitaker of Rowco Engineering Co. proposed a triangular font that allows "direct binary reading" to "permit both input and output from computers without respect to encoding matrices".
  • Some seven-segment display decoder chips (i.e., 74LS47) show unexpected output due to logic designed only to produce 0–9 correctly.

Sign

The hex system can express negative numbers the same way as in decimal, by putting a minus sign (−) before the number to indicate that it is negative.

Bit pattern

Hex can express the bit pattern in a processor, so a sequence of hex digits may represent a signed or even a floating-point value. This way, the negative number −4210 can be written as FFFF FFD6 in a 32-bit CPU register (in two's complement), as C228 0000 in a 32-bit FPU register or C045 0000 0000 0000 in a 64-bit FPU register (in the IEEE floating-point standard).

Exponential notation

Just as decimal numbers can be represented in exponential notation, so too can hex numbers. P notation uses the letter P (or p, for "power"), whereas E (or e) serves a similar purpose in decimal E notation. The number after the P is decimal and represents the binary exponent. Increasing the exponent by 1 multiplies by 2, not 16: 20p0 = 10p1 = 8p2 = 4p3 = 2p4 = 1p5. Usually, the number is normalized so that the hex digits start with 1. (zero is usually 0 with no P).

Example: 1.3DEp42 represents 1.3DE16 × 24210.

P notation is required by the IEEE 754-2008 binary floating-point standard and can be used for floating-point literals in the C99 edition of the C programming language. Using the %a or %A conversion specifiers, this notation can be produced by implementations of the printf family of functions following the C99 specification and Single Unix Specification (IEEE Std 1003.1) POSIX standard.

Sign in to keep reading

6 more chapters are free with a Lably account.

More in Basics