Hexadecimal
this wiki
Ad blocker interference detected!
Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers
Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.
In mathematics and computer science, hexadecimal, base-16, or simply hex, is a numeral system with a radix, or base, of 16, usually written using the symbols 0–9 and A–F, or a–f. For example, the decimal numeral 79, whose binary representation is 01001111, is 4F in hexadecimal (4 = 0100, F = 1111). IBM introduced the current hexadecimal system to the computing world; an earlier version, using the digits 0–9 and u–z, was introduced in 1956, and was used by the Bendix G-15 computer.
Uses
Hex | Bin | Dec |
---|---|---|
0 | 0000 | 0 |
1 | 0001 | 1 |
2 | 0010 | 2 |
3 | 0011 | 3 |
4 | 0100 | 4 |
5 | 0101 | 5 |
6 | 0110 | 6 |
7 | 0111 | 7 |
8 | 1000 | 8 |
9 | 1001 | 9 |
A | 1010 | 10 |
B | 1011 | 11 |
C | 1100 | 12 |
D | 1101 | 13 |
E | 1110 | 14 |
F | 1111 | 15 |
Hexadecimal is primarily used in computing to represent a byte, whose 256 possible values can be represented with only two digits in hexadecimal notation. Alternatively, representing a byte with 8-bit "ASCII" has a number of problems: first, there are unprintable control characters; second, ASCII itself stops at 7 bits with the remainder being system-specific extensions; and finally, even if all characters in the machine's set were displayable as something, neither users nor input methods are generally prepared to handle 256 unique characters.
Hex triplets
HTML and CSS use hexadecimal notation (hex triplets) to specify colors on web pages, with "#" standing for hexadecimal. Twenty-four-bit color is represented in the format #RRGGBB: where RR specifies the value of the Red component of the color, GG the Green component, and BB the Blue component. For example, a shade of red that is (238,9,63) in decimal is coded as #EE093F. This syntax is borrowed from the X Window System.
Example of conversion from hexadecimal triplet to decimal triplet: Hexadecimal triplet: FFCF4B
Step 1: Separate the triplets: FF CF 4B
Step 2: Convert each hexadecimal value to a decimal representation:
- FF = 15*16 + 15*1 = 255
- CF = 12*16 + 15*1 = 207
- 4B = 4*16 + 11*1 = 75
Result: Hexadecimal triplet FFCF4B = Decimal triplet 255,207,075
Other common uses
In URLs, all characters can be coded hexadecimally, even those not normally permitted. This is specified in RFC 3986. Each 2-digit (1 byte) hexadecimal sequence is preceded by a percent sign and refers to a specific UTF-8 character code. For example, in the URL http://en.wikipedia.org/wiki/Main%20Page, the (hexadecimal) UTF-8 character code for a space (" ") is 20.
The canonical written form of numeric IPv6 addresses represents each group of 16 bits as a separate hexadecimal numeral, to ease reading and transcription of the 128-bit addresses.
Some software programs will create unique order numbers by using a hexadecimal representation of the exact second the order was taken, based on the total number of seconds since the start of the 20th century. For example, C9BCE0F5 represents April 2, 2007 14:19:32.
Page numbers on teletext are written in hexadecimal, with available numbers being in the range of 100-8FF. However, page numbers with letters are only used for "hidden" and engineering pages.
In October 1996, Simon Plouffe, Peter Borwein and Jonathan Borwein created an equation that allows the nth digit of pi in hexadecimal to be calculated, without knowing any of the previous digits.^{[1]} The equation is given by:
Representing hexadecimal
Some hexadecimal representations are indistinguishable from decimal representations (to humans and computers); therefore, some convention is usually used to flag them.
In typeset text, hexadecimal is often indicated by a subscripted suffix such as 5A3_{16}, 5A3_{SIXTEEN}, or 5A3_{HEX}. In computer programming languages, alternatively—which are nearly always plain text without such typographical distinctions as subscript and superscript—,a wide variety of ways indicate hexadecimal representations; these are even seen in typeset text, especially in text that relates to a programming language.
The following are some of the most common representations:
- Ada and VHDL enclose hexadecimal numerals in based "numeric quotes", e.g.
16#5A3#
. (Note: Ada accepts this notation for all bases from 2 through 16 and for both integer and real types.) - C and languages with a similar syntax (such as C++, C#, Java and Javascript) prefix hexadecimal numerals with
0x
, e.g.0x5A3
. The leading0
is used so that the parser can simply recognize a number, and thex
stands for hexadecimal (cf.o
for Octal andb
for Binary). Thex
in0x
can be either in upper or lower case but is almost always seen written in lower case. - *nix shells use an escape character form
\x0FF
in expressions and0xFF
for constants. - In HTML, hexadecimal character references also use the x:
֣
should give the same as֣
– with your browser ֣ and ֣ respectively (Hebrew accent munah). Hexadecimal color references are prefixed with#
, e.g.#FFFFFF
(white). - Some assemblers indicate hex by an appended
h
(if the numeral starts with a letter, then also with a preceding 0, to indicate that it is a number), e.g.,0A3Ch
,5A3h
. The syntax may vary per assembly language. For example, the 6502 assembly language uses a $ as prefix, e.g. #$10FF (the # indicates a number). - Postscript indicates hex by a prefix
16#
. - Common Lisp use the prefixes
#x
and#16r
. - Pascal, other assemblers (AT&T, Motorola), and some versions of BASIC use a prefixed
$
, e.g.$5A3
. - The Smalltalk programming language uses the prefix
16r
. Note Smalltalk accepts the format<radix>r<digits>
where radix is a number base from 2 upwards (i.e. 2r1110 is 10r14 or 16rE), with the practical limitation being within the ASCII character set range 0-9 and A-Z used to represent the digits. Some versions of Smalltalk allow fractional digits following a period character,.
, enabling hexadecimal (and other base) representation of floating point numbers. - Some versions of BASIC, notably Microsoft's variants including QBasic and Visual Basic), prefix hexadecimal numerals with
&H
, e.g.&H5A3
; others such as BBC BASIC just used&
(used for octal in Microsoft's BASIC!). - TI 89 and 92 series designate 0h (ex 0hA3)
- Notations such as
X'5A3'
are sometimes seen; PL/I uses such notation. This is the most common format for hexadecimal on IBM mainframes (zSeries) and minicomputers (iSeries) running the traditional OS's (zOS, zVSE, zVM, TPF, OS/400), and is used in Assembler, PL/1, Cobol, JCL, scripts, commands and other places. The most common exceptions are when using a language with a different native convention (C, Java, etc.). This format was common on other (and now obsolete) IBM systems as well. - Microchip's MPASM assembler uses
H'5A'
to represent hexadecimal numbers as well as the more common 0x prefix. - Donald Knuth introduced the use of different fonts to represent radices in his book The TeXbook. In his notation, hexadecimal representations are written in a typewriter type, e.g. 5A3
There is no single agreed-upon standard, so all the above conventions are in use, sometimes even in the same paper. However, as they are quite unambiguous, little difficulty arises from this.
The most commonly used (or encountered) notations are the ones with a prefix "0x" or a subscript-base 16, for hex numbers. For example, both 0x2BAD and 2BAD_{16} represent the decimal number 11181 (or 11181_{10}).
The choice of the letters A through F to represent the additional digits was not universal in the early history of computers. During the 1950s, some installations favored using the digits 0 through 5 with a macron to indicate the values 10-15. Users of Bendix G-15 computers used the letters U through Z.
One solution how to write hexadecimal numbers distinctively is the use of figures that are made for the hexadecimal system but are not yet representable in Unicode.
Verbal representations
Not only are there currently no proper digits to represent the quantities from ten to fifteen (so letters are used as a substitute), but English also lacks a proper nomenclature to name hexadecimal numbers. Names such as "thirteen" and "fourteen" are decimal-based, and even though English has a few names for non-decimal powers —pair and score for the first binary power and the first vigesimal power, respectively; dozen, gross and great gross for the first three duodecimal(note that the term duodecimal itself is derived from the decimal system) powers—, no English name currently exists for any of the hexadecimal powers (corresponding to the decimal values 16, 256, 4096, 65536, 1048576, 16777216, etc.). So people have resorted to reading hexadecimal numbers by naming their digits (or digit-letters) individually in sequence in the same way as reading phone numbers (i.e., 4DA as "four-dee-aye"). However, the letter 'A' sounds similar to '8', 'C' sounds similar to '3', and 'D' can easily be mistaken for the 'ty' suffix as in "forty"; so 4DA could be mistaken for 48. To avoid misunderstandings, some convention must be established when exchanging hexadecimal numbers verbally, at least until a proper hexadecimal nomenclature is developed (if ever). To avoid confusion, the digits A-F are commonly pronounced with the NATO phonetic alphabet ("four-delta-alpha"), the World War II era Joint Army/Navy Phonetic Alphabet ("four-dog-able"), or some approximation of one of those systems.
The following defines another convention:
To pronounce a hexadecimal number when speaking, begin the number with an "x" prefix, where then any letters A-F all have an "x" suffix. Each digit or group of digits would be pronounced sequentially, where decimal expressions MUST NOT contain any letters. By starting with 'x', this alerts the listener to the use of the 'x' prefix/suffix convention and indicates that the number is hexadecimal.
The verbal hexadecimal characters are:
x 0 1 2 3 4 5 6 7 8 9 Ax Bx Cx Dx Ex Fx/F
The letters A-F are pronounced:
eɪ ɛks, bi ɛks, si ɛks, di ɛks, i ɛks, ɛf ɛks/ɛf
The hex number '34D' could be spoken as x 3 4 Dx.
This could also be spoken as x thirty-four Dx, but not as x three-hundred-forty Dx. An exception may be made for Fx, where it is pronounced ef. Sixteen is soluh in Hindi. The hex number '4D' could also be spoken as 'four soluh thirteen', but the hex number '34D' would not be called 'three hundred four soluh thirteen' because 'hundred' means ten tens, not sixteen sixteens, for which there is no convenient word.
Fractions
As with other numeral systems, the hexadecimal system can be used to represent rational numbers, although recurring digits are common since 16 has only a single prime factor:
1/1 | 1 | 1/5 | <center> = | 0.3 | 1/9 | <center> = | 0.1C7 | 1/D | <center> = | 0.13B | |
1/2 | <center> = | 0.8 | 1/6 | <center> = | 0.2A | 1/A | <center> = | 0.19 | 1/E | <center> = | 0.1249 |
1/3 | <center> = | 0.5 | 1/7 | <center> = | 0.249 | 1/B | <center> = | 0.1745D | 1/F | <center> = | 0.1 |
1/4 | <center> = | 0.4 | 1/8 | <center> = | 0.2 | 1/C | <center> = | 0.15 | 1/10 | <center> = | 0.1 |
Because the radix 16 is a square (4^{2}), hexadecimal fractions have an odd period much more often than decimal ones. Recurring digits occur when the denominator in lowest terms has a prime factor not found in the radix. Using hexadecimal notation, all fractions with denominators that are not a power of two will result in an infinite string of recurring digits. This makes hexadecimal (and binary) less convenient than decimal (let alone than duodecimal) to represent rational numbers, since a larger proportion of them lie outside its range of finite representation (all rational numbers finitely representable in hexadecimal are finitely representable in decimal and duodecimal, while only a fraction of those finitely representable in the latter ones are finitely representable in the former). Although hexadecimal is more efficient than either for the particular case of representing fractions with powers of two as the denominator (cf. one sixteenth is 0.1 in hexadecimal, 0.09 in duodecimal and 0.0625 in decimal).
Mapping to binary
Sometimes it is necessary to use binary data when working with computers, but it is difficult for humans to work with the large number of digits in binary. Although most humans are more 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 (4_{10}). The following is an example of converting 1111_{2} to base 10. Since each position in a binary numeral can only contain either a 1 or 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}
Therefore:
1111_{2} | = 8_{10} + 4_{10} + 2_{10} + 1_{10} |
= 15_{10} |
This example shows addition of 4 numbers; but with some practice, 1111_{2} can be mapped directly to F_{16} in one step (see table in Representing hexadecimal). The advantage of using hexadecimal rather than decimal increases with the size of the number. When the number becomes large, conversion to decimal becomes much more tedious; however, when mapping to hexadecimal, it is simple to divide the binary string into blocks of 4 positions and map each block of 4 bits to a single position hexadecimal digit.
This example shows the conversion of a binary number to decimal, mapping each digit to the decimal value, and adding the results.
</tr>
0 1 0 1 1 1 1 0 1 0 1 1 01 0 1 0 0 1 0 _{2} | = 262144_{10} + 65536_{10} + 32768_{10} + 16384_{10} + 8192_{10} + 2048_{10} + 512_{10} + 256_{10} + 64_{10} + 16_{10} + 2_{10} |
= 387922_{10} |
Compare this to the conversion to hexadecimal, where each group of four digits can be considered independently, and converted directly:
0 1 0 1 1 1 1 0 1 0 1 1 0 1 0 1 0 0 1 0_{2} | = | 0101_{ } | 1110_{ } | 1011_{ } | 0101_{ } | 0010_{2} |
= | 5 | E | B | 5 | 2_{16} | |
= | 5EB52_{16} |
Conversion from hexadecimal back to binary is just as direct.
The octal system can also be useful as a tool for people who need to deal directly with binary computer data, as in reading and understanding it. Compared to hexadecimal, octal represents data in blocks of 3 bits each, rather than 4.
One advantage of hexadecimal is that every unique 2-digit pair (or octet) always represents the same byte value. To "translate" a hexadecimal value into bytes, one needs only to separate the value into individual 2-digit groups, translate each group into its respective byte value, and then combine the results together to form an accurate translation of the entire original hexadecimal word. Conversely, bytes can also be easily translated into hexadecimal values by translating each byte individually into its hexadecimal 2-digit value, and then recombining the hexadecimal values into a "word". The resulting "word" will be an accurate hexadecimal representation of the original string of bytes.
Converting from other bases
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. Theoretically this is possible from any base but for most humans only decimal and for most computers only binary (which can be converted by far more efficient methods) 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 := (d-h_{i}) / 16
- If d = 0 (return series h_{i}) 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(d) { var r = d % 16; var result; if(d-r==0) result = toChar(r); else result = toHex( (d-r)/16 )+toChar(r); return result; } function toChar(n) { var alpha = "0123456789ABCDEF"; return alpha.charAt(n); }
Addition and multiplication in hexadecimal
It is also possible to make the conversion by assigning each place in the source base the hexadecimal representation of its place value and then performing multiplication and addition to get the final representation.
Conversion via binary
As computers generally work in binary the normal way for a computer to make such a conversion would be to convert to binary first (by doing multiplication and addition in binary) and then make use of the direct mapping from binary to hexadecimal.
Etymology
It was IBM that decided on the prefix of "hexa" rather than the proper Latin prefix of "sexa". The word "hexadecimal" is strange in that hexa is derived from the Greek έξ (hex) for "six" and decimal is derived from the Latin for "tenth". It may have been derived from the Latin root, but Greek deka is so similar to the Latin decem that some would not consider this nomenclature inconsistent. An older term was the incorrect Latin-like "sexidecimal" (correct Latin is "sedecim" for 16), but that was changed because some people thought it too risqué, and it also had an alternative meaning of "base 60". However, the word "sexagesimal" (base 60) retains the prefix. The earlier Bendix documentation used the term "sexadecimal". Donald Knuth has pointed out that the etymologically correct term is "senidenary", from the Latin term for "grouped by 16". (The terms "binary", "ternary" and "quaternary" are from the same Latin construction, and the etymologically correct term for "decimal" arithmetic should be "denary".)^{[2]} Schwartzman notes that the expected purely Latin form would be "sexadecimal", but then computer hackers would be tempted to shorten the word to "sex".^{[3]} Incidentally, the etymologically proper Greek term would be hexadecadic (although in Modern Greek deca-hexadic (δεκαεξαδικός) is more commonly used).
Humor
Hexadecimal is sometimes used in programmer jokes because certain words can be formed using only hexadecimal digits. Some of these words are "dead", "beef", "babe", and with appropriate substitutions "c0ffee". This is an example of such a joke. Since these are quickly recognisable by programmers, debugging setups sometimes initialize memory to them to help programmers see when something has not been initialized. Some people add an H after a number if they want to show that it is written in hexadecimal. In older Intel assembly syntax, this is sometimes the case. This may be the forerunner of the modern web parlance of "1337speak"
An example is the magic number in FAT Mach-O files and java programs, which is "CAFEBABE
". Single-architecture Mach-O files have the magic number "FEEDFACE
" at their beginning.
A Knuth reward check is one hexadecimal dollar, or $2.56.
The following table shows a joke in hexadecimal:
3x12=36 2x12=24 1x12=12 0x12=18
The first three are multiples of 12, while in the last one "0x12" in hex is 18.
0xdeadbeef is sometimes put into uninitialized memory.
Microsoft Windows XP clears its locked index.dat files with the hex codes: "0BADF00D"
Cultural references
In The Simpsons, on the episode Treehouse of Horror VI, where Homer enters the third dimension (Homer³), a hexadecimal string (46 72 69 6e 6b 20 52 75 6c 65 73 21) is floating in "3-D land" which, when used as character indices in the ASCII character set, translates to "Frink rules!" (excluding the quotes but including the exclamation mark).
In the TV show ReBoot there is a villainous character named Hexadecimal, who appears as a harlequin with constantly-changing masks, each with a different facial expression to represent differing emotional states.
In 1998, Subaru sold a special edition Impreza called the WRX-STi 22B. While some contend the name was derived from the use of a 2.2L motor ("22") and Bilstein brand ("B") suspension components, it has also been shown that "22B" is the hexadecimal equivalent of "555", where State Express 555 is the British American Tobacco brand that sponsored Subaru's early rally efforts.
See also
- Base32
- Base64
- Bubble Babble
- Hex editor
- Hexadecimal time
- Hexspeak
- Nibble — one hexadecimal digit can exactly represent one "nibble"
- Numeral system — a list of other base systems
- Binary numeral system
- HTML
References
- ↑ Bailey D, Borwein P, Plouffe S (1997). "On the rapid computation of various polylogarithmic constants". Mathematics of Computation 66 (218): 903–913.
- ↑ Knuth, Donald. (1969). Donald Knuth, in The Art of Computer Programming, Volume 2. ISBN 0-201-03802-1. (Chapter 17.)
- ↑ Schwartzman, S. (1994). The Words of Mathematics: an etymological dictionary of mathematical terms used in English. ISBN 0-88385-511-9.
External links
- Online Converter for Decimal/Hexadecimal Numerals (JavaScript, GPL)
- Intuitor Hex Headquarters - A site dedicated to changing the traditional base 10 (decimal) standard to hexadecimal.
- The hexadecimal metric system at hexadecimal.florencetime.net
- Leet Key, a Firefox extension that supports ASCII/Hex conversions and typing
- (Hexadecimal) web colors explained
ar:نظام عد سداسي عشر bs:Heksadecimalni sistem ca:Sistema hexadecimal cs:Hexadecimální číslo da:Hexadecimale talsystemel:Δεκαεξαδικό σύστημα αρίθμησης es:Sistema hexadecimal eo:Deksesuma sistemo eu:Zenbaki-sistema hamaseitargl:Código hexadecimal ko:십육진법 hr:Heksadekadski brojevni sustav id:Heksadesimal is:Sextándakerfið it:Sistema numerico esadecimale he:בסיס הקסדצימלי hu:Tizenhatos számrendszer nl:Hexadecimaalno:Sekstentallsystemet nn:Sekstentalssystemet pl:Szesnastkowy system liczbowy pt:Sistema hexadecimal ru:Шестнадцатеричная система счисления sk:Šestnástková sústava sl:Šestnajstiški številski sistem sr:Хексадецимални систем fi:Heksadesimaalijärjestelmä sv:Hexadecimala talsystemet th:เลขฐานสิบหก vi:Hệ thập lục phân tr:Heksadesimal uk:Шістнадцяткова система числення
yi:העקס