1
|
Hexadecimal is also the name of a character in the animated series ReBoot
| Numeral systems by culture | |
|---|---|
| Hindu-Arabic numerals | |
| Indian Eastern Arabic Khmer | Indian family Brahmi Thai |
| East Asian numerals | |
| Chinese Counting rods | Japanese Korean |
| Alphabetic numerals | |
| Abjad Armenian Cyrillic Ge\'ez | Hebrew Ionian/Greek Āryabhaṭa |
| Other systems | |
| Attic Babylonian Egyptian Etruscan | Mayan Roman Urnfield |
| List of numeral system topics | |
| Positional systems by base | |
| Decimal (10) | |
| 2, 4, 8, 16, 32, 64 | |
| 3, 9, 12, 20, 24, 30, 36, 60, more… | |
In mathematics and computer science, hexadecimal, base-16, or simply hexa or hex, is a numeral system with a radix, or base, of 16 (that is, it employs sixteen different symbols for digits), usually written using the symbols 0–9 to represent digits zero to nine, plus A, B, C, D, E, F (or alternatively a, b, c, d, e, f) to represent digits with values ten to fifteen. The first natural numbers written in hex this way are: 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F, 10, 11, 12, etc. Note that in hex, 10 represents sixteen, its base number.
Its primary purpose is to represent the binary code in a format easier for humans to read, and acts as a form of shorthand, in which one hexadecimal digit resembles a group of four contiguous binary bits, called a nibble, and thus is quite convenient to represent the state of any grouping of adjacent bits: 8, 16, 24, 32, 64, etc, inside a computer\'s memory, CPU ports and integrated circuits (IC) pins. For example, the decimal numeral 79, whose binary representation is 01001111, is 4F in hexadecimal (4 = 0100, F = 1111).
The hexadecimal numbering system is well-known and widely used in all fashion of computer and microprocessor programming environments and languages. IBM introduced the current hexadecimal system to the computing world; an earlier version, using the digits 0–9 and u–z, had been introduced in 1956, and had been used by the Bendix G-15 computer.
Contents |
| 0hex | = | 0dec | = | 0oct | 0 | 0 | 0 | 0 | |||
| 1hex | = | 1dec | = | 1oct | 0 | 0 | 0 | 1 | |||
| 2hex | = | 2dec | = | 2oct | 0 | 0 | 1 | 0 | |||
| 3hex | = | 3dec | = | 3oct | 0 | 0 | 1 | 1 | |||
| 4hex | = | 4dec | = | 4oct | 0 | 1 | 0 | 0 | |||
| 5hex | = | 5dec | = | 5oct | 0 | 1 | 0 | 1 | |||
| 6hex | = | 6dec | = | 6oct | 0 | 1 | 1 | 0 | |||
| 7hex | = | 7dec | = | 7oct | 0 | 1 | 1 | 1 | |||
| 8hex | = | 8dec | = | 10oct | 1 | 0 | 0 | 0 | |||
| 9hex | = | 9dec | = | 11oct | 1 | 0 | 0 | 1 | |||
| Ahex | = | 10dec | = | 12oct | 1 | 0 | 1 | 0 | |||
| Bhex | = | 11dec | = | 13oct | 1 | 0 | 1 | 1 | |||
| Chex | = | 12dec | = | 14oct | 1 | 1 | 0 | 0 | |||
| Dhex | = | 13dec | = | 15oct | 1 | 1 | 0 | 1 | |||
| Ehex | = | 14dec | = | 16oct | 1 | 1 | 1 | 0 | |||
| Fhex | = | 15dec | = | 17oct | 1 | 1 | 1 | 1 | |||
In computing, hexadecimal is primarily used to represent bytes. Attempts to represent the 256 possible byte values by other means have led to problems: First, there are a number of unprintable control characters in the standard 7-bit ASCII character set; second, extended (8-bit) ASCII sets may be system-specific (different computers may have different extended-character sets). Finally, even if there were a standard set of printable characters for every byte value, neither users nor input methods (e.g., the standard PC keyboard) are generally prepared to handle 256 unique characters. Most hex and disk editors include columns which represent as many bytes as possible using some given set of characters; the bytes which cannot be uniquely represented, are often filled with a dot (.).
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:
Result: Hexadecimal triplet FFCF4B = Decimal triplet 255,207,75
In URLs, all characters can be coded hexadecimally. This is specified in RFC 3986. Each 2-digit (1 byte) hexadecimal sequence is preceded by a percent sign. For example, in the URL http://en.wikipedia.orgMain%20Page, the (hexadecimal) UTF-8 character code for a space (" ") is 20.
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.
Many hexadecimal representations are indistinguishable from decimal representations (to humans and computers), namely numbers in which every digit is written with an Arabic digit: a given expression as 2001 can refer to a value of "two thousands one" (if decimal notation is used) or "eight thousands one hundred ninety three" (if hexadecimal notation is used). Therefore, some convention is usually used to flag them, in typeset text often indicated by a subscripted suffix such as 5A316, 5A3HEX or 5A3h. Programming languages have traditionally used plain text (i.e. ASCII), without such typographical distinctions as subscript and superscript, and have therefore instead denoted hexadecimal numbers using a wide variety of alternative conventions.
The following are some of the most common representations:
FFh or 0A3CH. Some allow numbers to start with a letter, some demand a leading zero for such cases.
$ as a prefix, e.g. $5A3.
H\'ABCD\' (for ABCD16), as well as the C-family convention (see below).
0x, e.g. 0x5A3; the leading 0 denotes a number and the x stands for hexadecimal. Inside literal characters and strings, a direct character code can be specified in hexadecimal by prefixing it with \x and giving always two hex digits, e.g. \'\x1B\' specifies a single ESC control character while "\x1B[0m\x1B[25;1H" is a string with the escape sequences used to clear the screen in an ANSI terminal. To output a value as hexadecimal with printf and related functions, the format sequences %X or %x are used (to display hex letters in upper or lowercase, respectively).
\x0FF in expressions and 0xFF for constants.
16#5A3#. (Ada accepts this notation for both integer and real types.)
8\'hFF, where 8 is the number of bits in the vector and FF is the hexadecimal constant.
16r. (Smalltalk accepts other bases than 16; some versions also accept fractional digits in bases other than 10.)
%, as in http://www.someweb.org/name%20with%20spaces where %20 is used in place of the space (blank) character, code number 20 in hex, 32 in decimal.
ꯍ. Hexadecimal color references are prefixed with #, e.g. #FFFFFF (white).
text/plain MIME-part body prefix non-printable ASCII characters with an equal to sign =, as in Espa=D1a to send "España" (Spain).
16#. Large portions of binary data (for example raw image pixels) can be entered as consecutive hexadecimal pairs written in ASCII, without prefixes: AA213FD51B3801043FBC...
#x and #16r.
&H, e.g. &H5A3. To format a value into a hexadecimal string, the function Hex$ is used: Hex$(1443) returns "5A3".
& (used for octal in Microsoft\'s BASIC).
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. This format was common on other (and now obsolete) IBM systems as well.
A hexadecimal multiplication table
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. Perhaps the most commonly encountered notation today is the "0x" notation, due to the widespread use of the C programming language and its descendants. A less C language-dependent (which not every programmer knows) and more human format is the "h" suffix: 20h represents 32 in decimal, while 20 simply represents twenty in decimal.
Also, there is no convention to use the lower or the upper case for the letters used as digits, but the later is prevalent, perhaps due to this way all digits shared the same tall, which is more common in typesetting. In every case, never upper and lower case are mixed in the same hex number: both ffff and FFFF are fine to represent the value 65,535, but neither Ffff, fFFf nor fFfF are allowed (although to computers this distinction is usually indifferent).
When used to show a whole number of a given number of bits (mainly 8, 16, 24, 32 and 64), it is usual to write all necessary leading zeros: 0F, 000F, 0000000F and so on. In these cases, digits are commonly arranged in groups of two or four, separated by a visual space: 00 00 0F or 0000 000F. Note that this visual grouping is used only to write hex numbers intended to be read by people, not machines.
Some CPU, as those of Intel, store whole and floating-point numbers internally in little endian format, where their contiguous bytes in memory are in reversed order respect the common use, e.g. D4 05 00 00 is the hex number 5D4 (1492 in decimal). Despite of that fact, hexadecimal numbers are never written in the little endian fashion, except where explicitly noted to show memory dumps.
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 for 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.
Other solutions have been proposed but not adopted.
Not only are there currently no proper digits to represent the quantities from ten to fifteen (so letters are used as a substitute), but the English language, and most Western European languages, also lack 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 for the first binary power; score for the first vigesimal power; dozen, gross and great gross for the first three duodecimal powers—no English name currently exists for any of the hexadecimal powers (corresponding to the decimal values 16, 256, 4096, 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.
In a strict sense, the hexadecimal system could be used to represent negative numbers the same way is done in decimal: –2A to represent –42 and so on.
But when used to show internal binary data of a computer, negative numbers are shown representing exactly the same bit patterns that the processors manage as negative numbers, that is, in two\'s complement for integers and following the IEEE 754 specification for floating-point (rational) numbers.
This way, the negative number –42 can be shown in hex 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.
So, in a broad sense, the hexadecimal system is used in computing exclusively to represent only positive numbers, and more specifically, streams of consecutive bytes (8-bit numbers) ranging positive values 0 to 255 (0h to FFh) each.
As with other numeral systems, the hexadecimal system can be used to represent rational numbers, although recurring digits are common since sixteen (10h) has only a single prime factor (two):
| 1⁄2 | 0.8 | 1⁄6 | 0.2AAAAAAAA... | 1⁄A | 0.1999999999... | 1⁄E | 0.1249249249... | ||||
| 1⁄3 | 0.5555555555... | 1⁄7 | 0.2492492492... | 1⁄B | 0.1745D1745D... | 1⁄F | 0.1111111111... | ||||
| 1⁄4 | 0.4 | 1⁄8 | 0.2 | 1⁄C | 0.1555555555... | 1⁄10 | 0.1 | ||||
| 1⁄5 | 0.3333333333... | 1⁄9 | 0.1C71C71C71... | 1⁄D | 0.13B13B13B1... | 1⁄11 | 0.0F0F0F0F0F... |
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; e.g., in base 3, we would count 0, 1, 2, 10 (three). Thus, whether we divide one by two for binary or divide one by sixteen for hexadecimal, both of these fractions are equal to 0.1 in each of these number systems. Because the radix 16 is a perfect square (4²), fractions expressed in hexadecimal have an odd period much more often than decimal ones, and there are no cyclic numbers (other than trivial single digits). Recurring digits occur 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 will result in an infinite string of recurring digits (e.g., thirds and fifths). This makes hexadecimal (and binary) less convenient than decimal (not to mention duodecimal and sexagesimal) for the purpose of representing rational numbers, since a larger proportion of them lie outside its range of finite representation. All rational numbers finitely representable in hexadecimal are also finitely representable in decimal, duodecimal and sexagesimal (i.e., any hexadecimal number with a finite number of digits will have a finite number of digits when expressed in those other bases), whereas only a fraction of those finitely representable in the latter ones are finitely representable in hexadeciaml (e.g., decimal 0.1, that is, the fraction one tenth, corresponds to the infinite recurring representation 0.199999999999... in hexadecimal). However, hexadecimal is more efficient than those other bases for the particular case of representing fractions with powers of two in the denominator (e.g., decimal one sixteenth is 0.1 in hexadecimal, 0.09 in duodecimal, 0;3,45 in sexagesimal and 0.0625 in decimal).
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 (410). The following is an example of converting 11112 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:
Therefore:
| 11112 | = 810 + 410 + 210 + 110 |
| = 1510 |
This example shows addition of 4 numbers; but with some practice, 11112 can be mapped directly to F16 in one step (see table in Uses). 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.
| 010111101011010100102 | = 26214410 + 6553610 + 3276810 + 1638410 + 819210 + 204810 + 51210 + 25610 + 6410 + 1610 + 210 |
| = 38792210 |
Compare this to the conversion to hexadecimal, where each group of four digits can be considered independently, and converted directly:
| 010111101011010100102 | = | 0101 | 1110 | 1011 | 0101 | 00102 |
| &;nbsp; | = | 5 | E | B | 5 | 216 |
| = | 5EB5216 | |||||
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.
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 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 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);
}
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.
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.
Most modern computer systems with windows-style graphical user interfaces provide some built-in calculator utility, capable of performing conversions between various radixes, generally including hexadecimal.
In Microsoft Windows, the Calculator utility can be set to scientific calculator mode, which allows conversions between radix 16 (hexadecimal), 10 (decimal), 8 (octal) and 2 (binary); the bases most commonly used by programmers. In Scientific Mode, the on screen simulated numeric keypad includes the hexadecimal digits A through F; which are only \'active\' when "Hex" is selected.
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 is "denary".)Knuth, Donald. (1969). Donald Knuth, in The Art of Computer Programming, Volume 2. ISBN 0-201-03802-1. (Chapter 17.) Schwartzman notes that the expected purely Latin form would be "sexadecimal", but then computer hackers would be tempted to shorten the word to "sex".Schwartzman, S. (1994). The Words of Mathematics: an etymological dictionary of mathematical terms used in English. ISBN 0-88385-511-9. Incidentally, the etymologically proper Greek term would be hexadecadic (although in Modern Greek deca-hexadic (δεκαεξαδικός) is more commonly used).
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". Since these are quickly recognizable 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. "Hexspeak" 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 interpreted as multiplication, but in the last, "0x" signals Hexadecimal interpretation of 12, which is 18.
0xdeadbeef is sometimes put into uninitialized memory.
Another joke based on the use of a word containing only letters from the first six in the alphabet (and thus those used in hexadecimal) is...
If only DEAD people understand hexadecimal, how many people understand hexadecimal?
In this case, DEAD refers to a hexadecimal number (57005 base 10), not the state of being no longer alive.
Microsoft Windows XP clears its locked index.dat files with the hex codes: "0BADF00D".
Two common bit patterns often employed to test hardware are 01010101 and 10101010 (their corresponding hex values are 55h and AAh, respectively). The reason for their use is the fact each bit will alternate from off (\'0\') to on (\'1\') or vice versa when switching between these two patterns. These two values are often used together as signatures in critical PC system sectors (e.g., the hex word, 0xAA55 which on little-endian systems is 55h followed by AAh, must always be found at the end of any valid Master Boot Record).
There have been occasional attempts to promote hexadecimal as the preferred numeral system. These attempts usually propose a separate pronunciation and/or symbology for the digits. Sometimes the proposal also attempts to unify standard measures so that they are multiples of 16.
An example of unifying standard measures is Hexadecimal time, proposing units of time based on subdividing a day by 16 so that there are 16 "hexhours" in a day.
To date it appears that there has not been widespread, or even partial, adoption of any of the unique symbology and pronunciation combinations proposed.
This article is licensed under the GNU Free Documentation License. It uses material from Wikipedia