Hexadecimal (hex for short) is a positional numeral system for representing a numeric value as base 16. For the most common convention, a digit is represented as "0" to "9" in the same way as decimal and as a letter of the alphabet from "A" to "F" (either upper or lower case) for the digits with decimal value 10 to 15.
As typical computer hardware is binary in nature and 16 is a power of 2, the hex representation is often used in computing as a dense representation of binary information. A hex digit represents 4 contiguous bitsknown as a nibble. An 8-bit byte is two hex digits, such as .
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 would be expressed in hex as . In computer programming, various notations are used. In C and many related languages, the prefix 0x is used. For example, 0x.
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.
00000000 57 69 6B 69 70 65 64 69 61 2C 20 74 68 65 20 66 00000010 72 65 65 20 65 6E 63 79 63 6C 6F 70 65 64 69 61 00000020 20 74 68 61 74 20 61 6E 79 6F 6E 65 20 63 61 6E 00000030 20 65 64 69 74 2C 20 69 6E 63 6C 75 64 69 6E 67 00000040 20 79 6F 75 20 28 61 6E 64 20 6D 65 29 21
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 : where 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 ode;, for instance T represents the character U+0054 (the uppercase letter "T"). If there is no the number is decimal (thus T is the same character).
In Intel-derived assembly languages and Modula-2, hex is denoted with a suffixed or : 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)
Other assembly languages (6502, Motorola), Pascal, Delphi, some versions of BASIC (Commodore), GameMaker Language, Godot and Forth use $ as a prefix: $5A3, $C1F27ED
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
The Icon and Smalltalk languages use the prefix 16r: 16r5A3
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,MSX is Coming — Part 2: Inside MSX Compute!, issue 56, January 1985, p. 52 QuickBASIC, FreeBASIC and Visual Basic prefix hex numbers with &H: &H5A3
BBC BASIC and Locomotive BASIC use & for hexBBC BASIC programs are not fully portable to Microsoft BASIC (without modification) since the latter takes & to prefix octal values. (Microsoft BASIC primarily uses &O to prefix octal, and it uses &H to prefix hex, but the ampersand alone yields a default interpretation as an octal prefix.
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: ,
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 followed by the hex value, e.g. 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 . CSS also allows 3-hexdigit abbreviations with one hexdigit per component: abbreviates , a golden orange.
In MIME (e-mail extensions) quoted-printable encoding, character codes are written as hex pairs prefixed with : is "España" (F1 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: ... 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: or abbreviated by removing leading zeros as (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 .
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 as , , , , and .
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". (1 page) (7 pages) Some seven-segment display decoder chips (i.e., 74LS47) show unexpected output due to logic designed only to produce 0–9 correctly. (29 pages)
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: . Usually, the number is normalized so that the hex digits start with (zero is usually with no P).
Example: represents .
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.
Verbal representation
Most European languages lack non-decimal-based words for some of the numerals eleven to fifteen. Some people read hex numbers digit by digit, like a phone number, or using the NATO phonetic alphabet, the Joint Army/Navy Phonetic Alphabet, or a similar ad hoc system. In the wake of the adoption of hex among IBM System/360 programmers, Magnuson (1968) suggested a pronunciation guide that gave short names to the letters of hex – for instance, "A" was pronounced "ann", B "bet", C "chris", etc. Another naming-system was published online by Rogers (2007) that tries to make the verbal representation distinguishable in any case, even when the actual number does not contain numbers A–F. Examples are listed in the tables below. Yet another naming system was elaborated by Babb (2015), based on a joke in Silicon Valley.
Others have proposed using the verbal Morse code conventions to express four-bit hex digits, with "dit" and "dah" representing zero and one, respectively, so that "0000" is voiced as "dit-dit-dit-dit" (....), "dah-dit-dit-dah" (-..-) voices the digit with a value of nine, and "dah-dah-dah-dah" (----) voices the hex digit for decimal 15.
Systems of counting on digits have been devised for both binary and hex. Arthur C. Clarke suggested using each finger as an on/off bit, allowing finger counting from zero to 102310 on ten fingers. Another system for counting up to FF16 (25510) is illustrated on the right.
+ Magnuson (1968)naming method Hex Name Decimal A ann 10 B bet 11 C chris 12 D dot 13 E ernest 14 F frost 15 1A annteen 26 A0 annty 160 5B fifty bet 91 A01C anntychristeen 40,988 1AD0 annteendotty 6,864 3A7D thirty annseventy dot 14,973