Hexadecimal


In mathematics and computing, hexadecimal is a positional system that represents numbers using a base of 16. Unlike the common way of representing numbers with ten symbols, it uses sixteen distinct symbols, most often the symbols "0"–"9" to represent values zero to nine, and "A"–"F" to represent values ten to fifteen.
Hexadecimal numerals are widely used by computer system designers and programmers, as they provide a human-friendly representation of binary-coded values. Each hexadecimal digit represents four binary digits, also known as a nibble, which is half a byte. For example, a single byte can have values ranging from 00000000 to 11111111 in binary form, which can be conveniently represented as 00 to FF in hexadecimal.
In mathematics, a subscript is typically used to specify the base, also known as the radix. For example, the decimal value would be expressed in hexadecimal as. In programming, a number of notations are used to support hexadecimal representation, usually involving a prefix or suffix. The prefix 0x is used in C and related languages, which would denote this value by 0x.
Hexadecimal is used in the transfer encoding Base16, in which each byte of the plaintext is broken into two 4-bit values and represented by two hexadecimal digits.

Representation

Written representation

Almost all modern use uses the letters A-F to represent the digits with values 10-15. 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 often used. Seven-segment displays use mixed-case AbCdEF to make digits that can be distinguished from each other.

Distinguishing from decimal

In contexts where the base is not clear, hexadecimal numbers can be ambiguous and confused with numbers expressed in other bases. There are several conventions for expressing values unambiguously. A numerical subscript can give the base explicitly: 15910 is decimal 159; 15916 is hexadecimal 159, which is equal to 34510. Some authors prefer a text subscript, such as 159decimal and 159hex, or 159d and 159h.
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 typewriter typeface: 5A3
In linear text systems, such as those used in most computer programming environments, a variety of methods have arisen:
The use of the letters A through F to represent the digits above 9 was not universal in the early history of computers.
There are no traditional numerals to represent the quantities from ten to fifteen – letters are used as a substitute – and most European languages lack non-decimal names for the numerals above ten. Even though English has names for several non-decimal powers, no English name describes the hexadecimal powers. Some people read hexadecimal 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 hexadecimal among IBM System/360 programmers, Robert A. Magnuson suggested in 1968 in Datamation Magazine a pronunciation guide that gave short names to the letters of hexadecimal - for instance, "A" was pronounced "ann", B "bet", C "chris", etc. Another naming system was invented independently by Tim Babb in 2015. An additional naming system has been published online by S. R. Rogers in 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.
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 is illustrated on the right.
NumberPronunciation
Aann
Bbet
Cchris
Ddot
Eernest
Ffrost
1Aannteen
A0annty
5Bfifty-bet
A01Cannty christeen
1AD0annteen dotty
3A7Dthirty-ann seventy-dot

Signs

The hexadecimal system can express negative numbers the same way as in decimal: −2A to represent −4210 and so on.
Hexadecimal can also be used to express the exact bit patterns used in the processor, so a sequence of hexadecimal 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, as C228 0000 in a 32-bit FPU register or C045 0000 0000 0000 in a 64-bit FPU register.

Hexadecimal exponential notation

Just as decimal numbers can be represented in exponential notation, so too can hexadecimal numbers. By convention, the letter P represents times two raised to the power of, whereas E serves a similar purpose in decimal as part of the E notation. The number after the P is decimal and represents the binary exponent. Increasing the exponent by 1 multiplies by 2, not 16. 10.0p1 = 8.0p2 = 4.0p3 = 2.0p4 = 1.0p5. Usually, the number is normalized so that the leading hexadecimal digit is 1.
Example: 1.3DEp42 represents.
Hexadecimal exponential notation is required by the IEEE 754-2008 binary floating-point standard.
This notation 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 POSIX standard.

Conversion

Binary conversion

Most computers manipulate binary data, but it is difficult for humans to work with a large number of digits for even a relatively small binary number. Although most humans are familiar with the base 10 system, it is much easier to map binary to hexadecimal than to decimal because each hexadecimal digit maps to a whole number of bits.
This example converts 11112 to base ten. Since each position in a binary numeral can contain either a 1 or a 0, its value may be easily determined by its position from the right:
Therefore:
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.

Other simple conversions

Although quaternary is little used, it can easily be converted to and from hexadecimal or binary. Each hexadecimal digit corresponds to a pair of quaternary digits and each quaternary digit corresponds to a pair of binary digits. In the above example 5 E B 5 216 = 11 32 23 11 024.
The octal 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.

Division-remainder in source base

As with all bases there is a simple algorithm for converting a representation of a number to hexadecimal by doing integer division and remainder operations in the source base. In theory, this is possible from any base, but for most humans only decimal and for most computers only binary can be easily handled with this method.
Let d be the number to represent in hexadecimal, and the series hihi−1...h2h1 be the hexadecimal digits representing the number.
  1. i ← 1
  2. hi ← d mod 16
  3. d ← / 16
  4. If d = 0 else increment i and go to step 2
"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 toHex
function toChar

Conversion through addition and multiplication

It is also possible to make the conversion by assigning each place in the source base the hexadecimal representation of its place value — before carrying out multiplication and addition to get the final representation.
For example, to convert the number B3AD to decimal, one can split the hexadecimal number into its digits: B, 3, A and D, and then get the final result by multiplying each decimal representation by 16p. In this case, we have that:
which is 45997 in base 10.

Tools for conversion

Most modern computer systems with graphical user interfaces provide a built-in calculator utility capable of performing conversions between the various radices, and in most cases would include the hexadecimal as well.
In Microsoft Windows, the Calculator utility can be set to Scientific mode, which allows conversions between radix 16, 10, 8 and 2, the bases most commonly used by programmers. In Scientific Mode, the on-screen numeric keypad includes the hexadecimal digits A through F, which are active when "Hex" is selected. In hex mode, however, the Windows Calculator supports only integers.

Elementary arithmetic

Elementary operations such additions, subtractions, multiplications and divisions can be carried out indirectly through conversion to an alternate numeral system, such as the decimal system, since it is the most commonly adopted system, or the binary system, since each hex digit corresponds to four binary digits,
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.

Real numbers

Rational numbers

As with other numeral systems, the hexadecimal system can be used to represent rational numbers, although repeating expansions are common since sixteen has only a single prime factor; two.
For any base, 0.1 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 perfect square, fractions expressed in hexadecimal have an odd period much more often than decimal ones, and there are no cyclic numbers. 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. This makes hexadecimal less convenient than decimal for representing rational numbers since a larger proportion lie 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 is equivalent to 0.116, 0.0912, and 0;3,4560.

Irrational numbers

The table below gives the expansions of some common irrational numbers in decimal and hexadecimal.

Powers

Powers of two have very simple expansions in hexadecimal. The first sixteen powers of two are shown below.
2xValueValue
2011
2122
2244
2388
2410hex16dec
2520hex32dec
2640hex64dec
2780hex128dec
28100hex256dec
29200hex512dec
2A 400hex1024dec
2B 800hex2048dec
2C 1000hex4096dec
2D 2000hex8192dec
2E 4000hex16,384dec
2F 8000hex32,768dec
210 10000hex65,536dec

Cultural

Etymology

The word hexadecimal is composed of hexa-, derived from the Greek ἕξ for six, and -decimal, derived from the Latin for tenth. Webster's Third New International online derives hexadecimal as an alteration of the all-Latin sexadecimal. The earliest date attested for hexadecimal in Merriam-Webster Collegiate online is 1954, placing it safely in the category of international scientific vocabulary. It is common in ISV to mix Greek and Latin combining forms freely. The word sexagesimal retains the Latin prefix. Donald Knuth has pointed out that the etymologically correct term is senidenary, from the Latin term for grouped by 16. 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". Schwartzman notes that the expected form from usual Latin phrasing would be sexadecimal, but computer hackers would be tempted to shorten that word to sex. The etymologically proper Greek term would be hexadecadic / ἑξαδεκαδικός / hexadekadikós.

Use in Chinese culture

The traditional Chinese units of measurement were base-16. For example, one jīn in the old system equals sixteen taels. The suanpan can be used to perform hexadecimal calculations such as additions and subtractions.

Primary numeral system

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 example of unified standard measures is hexadecimal time, which subdivides a day by 16 so that there are 16 "hexhours" in a day.

Base16 (transfer encoding)

Base16 can also refer to a binary to text encoding belonging to the same family as Base32, Base58, and Base64.
In this case, data is broken into 4-bit sequences, and each value is encoded using 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' 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 URL 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.