# 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: 159_{10}is decimal 159; 159

_{16}is hexadecimal 159, which is equal to 345

_{10}. Some authors prefer a text subscript, such as 159

_{decimal}and 159

_{hex}, or 159

_{d}and 159

_{h}.

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:

- Unix shells, AT&T assembly language and likewise the C programming language use the prefix
`0x`

for numeric constants represented in hex:`0x5A3`

. Character and string constants may express character codes in hexadecimal with the prefix`\x`

followed by two hex digits:`'\x1B'`

represents the Esc control character;`"\x1B0m\x1B[25;1H"`

is a string containing 11 characters with two embedded Esc characters. To output an integer as hexadecimal with the [printf function family, the format conversion code`%X`

or`%x`

is used. - In URIs, character codes are written as hexadecimal pairs prefixed with
`%`

:

wherehttp://www.example.com/name%20with%20spaces `%20`

is the code for the space character, ASCII code point 20 in hex, 32 in decimal. - In XML and XHTML, characters can be expressed as hexadecimal numeric character references using the notation
`&#x`

, for instance*code*;`’`

represents the character U+2019. If there is no the number is decimal. - In the Unicode standard, a character value is represented with
`U+`

followed by the hex value, e.g.`U+20AC`

is the Euro sign. - Color references in HTML, CSS and X Window can be expressed with six hexadecimal digits prefixed with
`#`

: white, for example, is represented as`#FFFFFF`

. CSS also allows 3-hexdigit abbreviations with one hexdigit per component: #FA3 abbreviates #FFAA33. - In MIME quoted-printable encoding, character codes are written as hexadecimal pairs prefixed with
`=`

:`Espa=F1a`

is "España" - In Intel-derived assembly languages and Modula-2, hexadecimal is denoted with a suffixed
`H`or`h`:`FFh`

or`05A3H`

. Some implementations require a leading zero when the first hexadecimal digit character is not a decimal digit, so one would write`0FFh`

instead of`FFh`

- Other assembly languages, Pascal, Delphi, some versions of BASIC, GameMaker Language, Godot and Forth use
`$`

as a prefix:`$5A3`

. - Some assembly languages use the notation
`H'ABCD'`

. Similarly, Fortran 95 uses Z'ABCD'. - Ada and VHDL enclose hexadecimal numerals in based "numeric quotes":
`16#5A3#`

. For bit vector constants VHDL uses the notation`x"5A3"`

. - Verilog represents hexadecimal constants in the form
`8'hFF`

, where 8 is the number of bits in the value and FF is the hexadecimal constant. - The Smalltalk language uses the prefix
`16r`

:`16r5A3`

- PostScript and the Bourne shell and its derivatives denote hex with prefix
`16#`

:`16#5A3`

. For PostScript, binary data can be expressed as unprefixed consecutive hexadecimal pairs:`AA213FD51B3801043FBC`

... - 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 Hexadecimal number representation for reading and printing numbers. Thus Hexadecimal 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 hexadecimal numbers with
`&H`

:`&H5A3`

- BBC BASIC and Locomotive BASIC use
`&`

for hex. - TI-89 and 92 series uses a
`0h`

prefix:`0h5A3`

- ALGOL 68 uses the prefix
`16r`

to denote hexadecimal numbers:`16r5a3`

. Binary, quaternary and octal numbers can be specified similarly. - The most common format for hexadecimal on IBM mainframes and midrange computers running the traditional OS's is
`X'5A3'`

, and is used in Assembler, PL/I, COBOL, JCL, scripts, commands and other places. This format was common on other IBM systems as well. Occasionally quotation marks were used instead of apostrophes. - Any IPv6 address can be written as eight groups of four hexadecimal digits, where each group is separated by a colon. This, for example, is a valid IPv6 address: or abbreviated by removing zeros as .
- Globally unique identifiers are written as thirty-two hexadecimal digits, often in unequal hyphen-separated groupings, for example.
### History of written representations

*A*through

*F*to represent the digits above 9 was not universal in the early history of computers.

- 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,,,, and.
- The SWAC and Bendix G-15 computers used the lowercase letters
*u*,*v*,*w*,*x*,*y*and*z*for the values 10 to 15. - The ILLIAC I computer used the uppercase letters
*K*,*S*,*N*,*J*,*F*and*L*for the values 10 to 15. - The Librascope LGP-30 used the letters
*F*,*G*,*J*,*K*,*Q*and*W*for the values 10 to 15. - The Honeywell Datamatic D-1000 used the lowercase letters
*b*,*c*,*d*,*e*,*f*, and*g*whereas the Elbit 100 used the uppercase letters*B*,*C*,*D*,*E*,*F*and*G*for the values 10 to 15. - The Monrobot XI used the letters
*S*,*T*,*U*,*V*,*W*and*X*for the values 10 to 15. - The NEC parametron computer NEAC 1103 used the letters
*D*,*G*,*H*,*J*,*K*for values 10–15. - The Pacific Data Systems 1020 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. This notation did not become very popular.
- 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, which did not gain much acceptance.
- Some seven-segment display decoder chips show the random result of logic designed only to produce 0-9 correctly.
### Verbal and digital representations

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 1023

_{10}on ten fingers. Another system for counting up to FF

_{16}is illustrated on the right.

Number | Pronunciation |

A | ann |

B | bet |

C | chris |

D | dot |

E | ernest |

F | frost |

1A | annteen |

A0 | annty |

5B | fifty-bet |

A01C | annty christeen |

1AD0 | annteen dotty |

3A7D | thirty-ann seventy-dot |

### Signs

The hexadecimal system can express negative numbers the same way as in decimal: −2A to represent −42_{10}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 −42

_{10}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 1111

_{2}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:

- 0001
_{2}= 1_{10} - 0010
_{2}= 2_{10} - 0100
_{2}= 4_{10} - 1000
_{2}= 8_{10}

With little practice, mapping 1111

_{2}to F

_{16}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 2_{16}= 11 32 23 11 02

_{4}.

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 h

_{i}h

_{i−1}...h

_{2}h

_{1}be the hexadecimal digits representing the number.

- i ← 1
- h
_{i}← d mod 16 - d ← / 16
- If d = 0 else increment i and go to step 2

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 16

^{p}. 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.0625

_{10}is equivalent to 0.1

_{16}, 0.09

_{12}, and 0;3,45

_{60}.

### 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.2^{x} | Value | Value |

2^{0} | 1 | 1 |

2^{1} | 2 | 2 |

2^{2} | 4 | 4 |

2^{3} | 8 | 8 |

2^{4} | 10_{hex} | 16_{dec} |

2^{5} | 20_{hex} | 32_{dec} |

2^{6} | 40_{hex} | 64_{dec} |

2^{7} | 80_{hex} | 128_{dec} |

2^{8} | 100_{hex} | 256_{dec} |

2^{9} | 200_{hex} | 512_{dec} |

2^{A} | 400_{hex} | 1024_{dec} |

2^{B} | 800_{hex} | 2048_{dec} |

2^{C} | 1000_{hex} | 4096_{dec} |

2^{D} | 2000_{hex} | 8192_{dec} |

2^{E} | 4000_{hex} | 16,384_{dec} |

2^{F} | 8000_{hex} | 32,768_{dec} |

2^{10} | 10000_{hex} | 65,536_{dec} |

## 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:

- Most programming languages already have facilities to parse ASCII-encoded hexadecimal
- Being exactly half a byte, 4-bits is easier to process than the 5 or 6 bits of Base32 and Base64 respectively
- The symbols 0-9 and A-F are universal in hexadecimal notation, so it is easily understood at a glance without needing to rely on a symbol lookup table
- Many CPU architectures have dedicated instructions that allow access to a half-byte, making it more efficient in hardware than Base32 and Base64

- Space efficiency is only 50%, since each 4-bit value from the original data will be encoded as an 8-bit byte. In contrast, Base32 and Base64 encodings have a space efficiency of 63% and 75% respectively.
- Possible added complexity of having to accept both uppercase and lowercase letters