Hexadecimal (also known as base-16 or simply hex) is a positional numeral system that represents numbers using a radix (base) of sixteen. Unlike the decimal system representing numbers using ten symbols, hexadecimal uses sixteen distinct symbols, most often the symbols "0"–"9" to represent values 0 to 9 and "A"–"F" to represent values from ten to fifteen.
Software developers and system designers widely use hexadecimal numbers because they provide a convenient representation of binary code values. Each hexadecimal digit represents four (binary digits), also known as a nibble (or nybble). For example, an 8-bit byte is two hexadecimal digits and its value can be written as to in hexadecimal.
In mathematics, a subscript is typically used to specify the base. For example, the decimal value would be expressed in hexadecimal as . In programming, several notations denote hexadecimal numbers, usually involving a prefix. The prefix 0x is used in C, which would denote this value as 0x{{hexadecimal|711|no}}.
Hexadecimal is used in the transfer encoding Base 16, in which each byte of the plaintext is broken into two 4-bit values and represented by two hexadecimal digits.
There is no universal convention to use lowercase or uppercase, so each is prevalent or preferred in particular environments by community standards or convention; even mixed case is used. Some seven-segment displays use mixed-case 'A b C d E F' to distinguish the digits A–F from one another and from 0–9.
There is some standardization of using spaces (rather than commas or another punctuation mark) to separate hex values in a long list. 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.
Donald Knuth introduced the use of a particular typeface to represent a particular radix in his book The TeXbook. Hexadecimal representations are written there in a Monospaced font: ,
In linear text systems, such as those used in most computer programming environments, a variety of methods have arisen:
Others have proposed using the verbal Morse Code conventions to express four-bit hexadecimal 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 hexadecimal digit for decimal 15.
Systems of counting on digits have been devised for both binary and hexadecimal. 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 ! Number !! Pronunciation !! Decimal Value |
10 |
11 |
12 |
13 |
14 |
15 |
26 |
160 |
91 |
40,990 |
6,864 |
14,973 |
+ Rogers (2007) naming method ! Number !! Pronunciation !! Decimal Value |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
31 |
80 |
192 |
256 |
4,096 |
62 |
225 |
3,146 |
5,955 |
+ Atkins-Bittner (2015) naming method | ||
A | ae | 10 |
B | bee | 11 |
C | cee | 12 |
D | dee | 13 |
E | ee | 14 |
F | eff | 15 |
A0 | atta | 160 |
B0 | bitta | 176 |
C0 | citta | 192 |
D0 | dickety | 208 |
E0 | eckity | 224 |
F0 | fleventy | 240 |
1A | abteen | 26 |
1B | bibteen | 27 |
1C | cibteen | 28 |
1D | dibbleteen | 29 |
1E | ebbleteen | 30 |
1F | fleventeen | 31 |
100 | one bitey | 256 |
10,000 | one millby | 65,536 |
100,000,000 | one billby | 4,294,967,296 |
Hexadecimal can also be used to express the exact bit patterns used in the processor, so a sequence of hexadecimal digits may represent a signedness 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).
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.
Therefore:
= 810 + 410 + 210 + 110 |
= 1510 |
With little practice, mapping 11112 to F16 in one step becomes easy (see table in written representation). The advantage of using hexadecimal rather than decimal increases rapidly with the size of the number. When the number becomes large, conversion to decimal is very tedious. However, when mapping to hexadecimal, it is trivial to regard the binary string as 4-digit groups and map each to a single hexadecimal digit.
This example shows the conversion of a binary number to decimal, mapping each digit to the decimal value, and adding the results.
Compare this to the conversion to hexadecimal, where each group of four digits can be considered independently and converted directly:
The conversion from hexadecimal to binary is equally direct.
The octal (base 8) system can also be converted with relative ease, although not quite as trivially as with bases 2 and 4. Each octal digit corresponds to three binary digits, rather than four. Therefore, we can convert between octal and hexadecimal via an intermediate conversion to binary followed by regrouping the binary digits in groups of either three or four.
Let d be the number to represent in hexadecimal, and the series hihi−1...h2h1 be the hexadecimal digits representing the number.
"16" may be replaced with any other base that may be desired.
The following is a JavaScript implementation of the above algorithm for converting any number to a hexadecimal in String representation. Its purpose is to illustrate the above algorithm. To work with data seriously, however, it is much more advisable to work with bitwise operators.
function toChar(n) {
which is 45997 in base 10.
In Microsoft Windows, the Calculator, on its Programmer mode, allows conversions between hexadecimal and other common programming bases.
Alternatively, one can also perform elementary operations directly within the hex system itself — by relying on its addition/multiplication tables and its corresponding standard algorithms such as long division and the traditional subtraction algorithm.
For any base, 0.1 (or "1/10") is always equivalent to one divided by the representation of that base value in its own number system. Thus, whether dividing one by two for binary or dividing one by sixteen for hexadecimal, both of these fractions are written as 0.1. Because the radix 16 is a square number (42), fractions expressed in hexadecimal have an odd period much more often than decimal ones, and there are no (other than trivial single digits). Recurring digits are exhibited when the denominator in lowest terms has a prime factor not found in the radix; thus, when using hexadecimal notation, all fractions with denominators that are not a power of two result in an infinite string of recurring digits (such as thirds and fifths). This makes hexadecimal (and binary) less convenient than decimal for representing rational numbers since a larger proportion lies outside its range of finite representation.
All rational numbers finitely representable in hexadecimal are also finitely representable in decimal, duodecimal and sexagesimal: that is, any hexadecimal number with a finite number of digits also has a finite number of digits when expressed in those other bases. Conversely, only a fraction of those finitely representable in the latter bases are finitely representable in hexadecimal. For example, decimal 0.1 corresponds to the infinite recurring representation 0.1 in hexadecimal. However, hexadecimal is more efficient than duodecimal and sexagesimal for representing fractions with powers of two in the denominator. For example, 0.062510 (one-sixteenth) is equivalent to 0.116, 0.0912, and 0;3,4560.
As with the duodecimal system, there have been occasional attempts to promote hexadecimal as the preferred numeral system. These attempts often propose specific pronunciation and symbols for the individual numerals. Some proposals unify standard measures so that they are multiples of 16.
An early such proposal was put forward by John W. Nystrom in Project of a New System of Arithmetic, Weight, Measure and Coins: Proposed to be called the Tonal System, with Sixteen to the Base, published in 1862.
Nystrom among other things suggested hexadecimal time, which subdivides a day by 16,
so that there are 16 "hours" (or "10 tims", pronounced tontim) in a day.Nystrom (1862), p. 33:
"In expressing time, angle of a circle, or points on the compass, the unit tim should be noted as integer, and parts thereof as tonal fractions, as 5·86 tims is five times and metonby *"sutim ]."
The word hexadecimal is first recorded in 1952.C. E. Fröberg, Hexadecimal Conversion Tables, Lund (1952). It is macaronic in the sense that it combines Greek language ἕξ (hex) "six" with -decimal.
The all-Latin alternative sexadecimal (compare the word sexagesimal for base 60) is older, and sees at least occasional use from the late 19th century.
The Century Dictionary of 1895 has sexadecimal in the more general sense of "relating to sixteen".
An early explicit use of sexadecimal in the sense of "using base 16" is found also in 1895, in the Journal of the American Geographical Society of New York, vols. 27–28, p. 197.
It is still in use in the 1950s in Bendix documentation.
Schwartzman (1994) argues that use of sexadecimal may have been avoided because of its suggestive abbreviation to sex.
Terminology and notation did not become settled until the end of the 1960s.
In 1969, Donald Knuth argued that the etymologically correct term would be senidenary, or possibly sedenary, a Latinate term intended to convey "grouped by 16" modelled on binary, ternary, quaternary, etc.
According to Knuth's argument, the correct terms for decimal and octal arithmetic would be denary and octonary, respectively.Knuth, Donald. (1969). The Art of Computer Programming, Volume 2. . (Chapter 17.)
Alfred B. Taylor used senidenary in his mid-1800s work on alternative number bases, although he rejected base 16 because of its "incommodious number of digits".Alfred B. Taylor, Report on Weights and Measures, Pharmaceutical Association, 8th Annual Session, Boston, 15 September 1859. See pages and 33 and 41.Alfred B. Taylor, "Octonary numeration and its application to a system of weights and measures", Proc Amer. Phil. Soc. Vol XXIV , Philadelphia, 1887; pages 296–366. See pages 317 and 322.
The now-current notation using the letters A to F establishes itself as the de facto standard beginning in 1966, in the wake of the
publication of the Fortran IV manual for IBM System/360, which (unlike earlier variants of Fortran) recognizes a standard for entering hexadecimal constants. IBM System/360 FORTRAN IV Language (1966), p. 13.
As noted above, alternative notations were used by NEC (1960) and The Pacific Data Systems 1020 (1964). The standard adopted by IBM seems to have become widely adopted by 1968, when Bruce Alan Martin
in his letter to the editor of the CACM complains that
Martin's argument was that use of numerals 0 to 9 in nondecimal numbers "imply to us a base-ten place-value scheme":
"Why not use entirely new symbols (and names) for the seven or fifteen nonzero digits needed in octal or hex. Even use of the letters A through P would be an improvement, but entirely new symbols could reflect the binary nature of the system".
He also argued that "re-using alphabetic letters for numerical digits represents a gigantic backward step from the invention of distinct, non-alphabetic glyphs for numerals sixteen centuries ago" (as Brahmi numerals, and later in a Hindu–Arabic numeral system),
and that the recent ASCII standards (ASA X3.4-1963 and USAS X3.4-1968)
"should have preserved six code table positions following the ten decimal digits
-- rather than needlessly filling these with punctuation characters"
(":;<=>?") that might have been placed elsewhere among the 128 available positions.
In this case, data is broken into 4-bit sequences, and each value (between 0 and 15 inclusively) is encoded using one of 16 symbols from the ASCII character set. Although any 16 symbols from the ASCII character set can be used, in practice, the ASCII digits "0"–"9" and the letters "A"–"F" (or the lowercase "a"–"f") are always chosen in order to align with standard written notation for hexadecimal numbers.
There are several advantages of Base16 encoding:
The main disadvantages of Base16 encoding are:
Support for Base16 encoding is ubiquitous in modern computing. It is the basis for the W3C standard for Percent-encoding, where a character is replaced with a percent sign "%" and its Base16-encoded form. Most modern programming languages directly include support for formatting and parsing Base16-encoded numbers.
= 51210 + 6410 + 1610 + 810 + 410 = 60410 11002 C16 25C16
Other simple conversions
Division-remainder in source base
var r = d % 16;
if (d - r == 0) {
return toChar(r);
}
return toHex((d - r) / 16) + toChar(r);
}
const alpha = "0123456789ABCDEF";
return alpha.charAt(n);
}
Conversion through addition and multiplication
Tools for conversion
Elementary arithmetic
Real numbers
Rational numbers
2 1/2 0.5 0.8 1/2 3 1/3 0.3333... = 0. 0.5555... = 0. 1/3 4 1/4 0.25 0.4 1/4 5 1/5 0.2 0. 1/5 6 1/6 , 0.1 0.2 , 1/6 7 1/7 7 0. 0. 7 1/7 8 1/8 0.125 0.2 1/8 9 1/9 0. 0. 1/9 10 1/10 , 0.1 0.1 , 1/A 11 1/11 0. 0. B 1/B 12 1/12 , 0.08 0.1 , 1/C 13 1/13 13 0. 0. D 1/D 14 1/14 , 7 0.0 0.1 , 7 1/E 15 1/15 , 0.0 0. , 1/F 16 1/16 0.0625 0.1 1/10 17 1/17 17 0. 0. 1/11 18 1/18 , 0.0 0.0 , 1/12 19 1/19 19 0. 0. 13 1/13 20 1/20 , 0.05 0.0 , 1/14 21 1/21 , 7 0. 0. , 7 1/15 22 1/22 , 0.0 0.0 , B 1/16 23 1/23 23 0. 0. 17 1/17 24 1/24 , 0.041 0.0 , 1/18 25 1/25 0.04 0. 1/19 26 1/26 , 13 0.0 0.0 , D 1/1A 27 1/27 0. 0. 1/1B 28 1/28 , 7 0.03 0.0 , 7 1/1C 29 1/29 29 0. 0. 1D 1/1D 30 1/30 , , 0.0 0.0 , , 1/1E 31 1/31 31 0. 0. 1F 1/1F 32 1/32 0.03125 0.08 1/20 33 1/33 , 0. 0. , B 1/21 34 1/34 , 17 0.0 0.0 , 1/22 35 1/35 , 7 0.0 0. , 7 1/23 36 1/36 , 0.02 0.0 , 1/24 37 1/37 37 0. 0. 25 1/25 38 1/38 , 19 0.0 0.0 , 13 1/26 39 1/39 , 13 0.0 0.0 , D 1/27 40 1/40 , 0.025 0.0 , 1/28 41 1/41 41 0.0 0.0 29 1/29 42 1/42 , , 7 0.0 0.0 , , 7 1/2A 43 1/43 43 0.0 0.0 2B 1/2B 44 1/44 , 0.02 0.05 , B 1/2C 45 1/45 , 0.0 0.0 , 5 1/2D 46 1/46 , 23 0.0 0.0 , 17 1/2E 47 1/47 47 0.0 0.0 2F 1/2F 48 1/48 , 0.0208 0.0 , 1/30
Irrational numbers
(the length of the diagonal of a unit square) ... 1.6A09E667F3BCD... (the length of the diagonal of a unit cube) ... 1.BB67AE8584CAA... (the length of the diagonal of a 1×2 rectangle) ... 2.3C6EF372FE95... (phi, the golden ratio = ) ... 1.9E3779B97F4A... (pi, the ratio of circumference to diameter of a circle)
...3.243F6A8885A308D313198A2E0
3707344A4093822299F31D008...(the base of the natural logarithm) ... 2.B7E151628AED2A6B... (the Thue–Morse constant) ... 0.6996 9669 9669 6996... (the limiting difference between the harmonic series and the natural logarithm) ... 0.93C467E37DB0C7A4D1B...
Powers
1 2 4 8 16dec 32dec 64dec 128dec 256dec 512dec 1,024dec 2,048dec 4,096dec 8,192dec 16,384dec 32,768dec 65,536dec
Cultural history
Base16 (transfer encoding)
See also
|
|