Hexadecimal 13263 226172197 2008-07-17T03:58:04Z Escarbot 1554195 robot Adding: [[fa:هگزادسیمال]] :{{dablink|[[List_of_ReBoot_characters#Hexadecimal|Hexadecimal]] is also the name of a character in the animated series [[ReBoot]]}} {{Numeral systems}} In [[mathematics]] and [[computer science]], '''hexadecimal''' (also ''[[Base (mathematics)|base]]-{{num|16}}'', '''hexa''', or '''hex''') is a [[numeral system]] with a [[radix]], or base, of 16. It uses sixteen distinct symbols, most often the symbols '''0'''&ndash;'''9''' to represent values zero to nine, and ''A'', ''B'', ''C'', ''D'', ''E'', ''F'' (or ''a'' through ''f'') to represent values ten to fifteen. Its primary use is as a human friendly representation of [[binary code]]d values, so it is often used in digital electronics and computer engineering. Since each hexadecimal digit represents four binary digits ([[bit]]s)—also called a [[nibble]]—it is a compact and easily translated [[shorthand]] to express values in [[base two]]. [[IBM]] introduced the current hexadecimal system to the computing world; an earlier version, using the digits ''0''&ndash;''9'' and ''U''&ndash;''Z'', was introduced in 1956 by the [[Bendix G-15]] computer.{{Fact|date=March 2008}} __TOC__ {{clr}} ==Uses== <table border=" cellspacing="0" cellpadding="10" align="right"> <tr> <td> {| border="0" cellspacing="0" cellpadding="0" style="text-align:center;border:2px" align="center" |- style="background:black; height:2px" | style="background:black; width:2px" | || || || || || || || || || || || |-style="background:#F6A07C; color:black; height:24px" | style="background:black; width:2px" | || style="width:45px"|'''0'''<sub>hex</sub> || = || style="width:45px"|[[0 (number)|0<sub>dec</sub>]] || = || style="width:45px"|0<sub>oct</sub> ||style="background:black; width:2px" | || style="background:white;width:24px"|0|| style="background:white;width:24px"|0|| style="background:white;width:24px"|0|| style="background:white;width:24px"|0 ||style="background:black; width:2px" | |-style="background:#E3EDE9; color:black; height:24px" | style="background:black; width:2px" | || '''1'''<sub>hex</sub> || = || [[1 (number)|1<sub>dec</sub>]] || = || 1<sub>oct</sub> ||style="background:black; width:2px" | || style="background:white;width:24px"|0|| style="background:white;width:24px"|0|| style="background:white;width:24px"|0|| style="background:red;width:24px"|1||style="background:black; width:2px" | |-style="background:#E3EDE9; color:black; height:24px" | style="background:black; width:2px" | || '''2'''<sub>hex</sub> || = || [[2 (number)|2<sub>dec</sub>]] ||= || 2<sub>oct</sub> ||style="background:black; width:2px" | || style="background:white;width:24px"|0|| style="background:white;width:24px"|0|| style="background:red;width:24px"|1|| style="background:white;width:24px"|0||style="background:black; width:2px" | |-style="background:#FDC888; color:black; height:24px" | style="background:black; width:2px" | || '''3'''<sub>hex</sub> || = || [[3 (number)|3<sub>dec</sub>]] || = || 3<sub>oct</sub> ||style="background:black; width:2px" | || style="background:white;width:24px"|0|| style="background:white;width:24px"|0|| style="background:red;width:24px"|1|| style="background:red;width:24px"|1||style="background:black; width:2px" | |- style="background:black; height:2px" | style="background:black; width:2px" | || || || || || || || || || || || |-style="background:#E3EDE9; color:black; height:24px" | style="background:black; width:2px" | || '''4'''<sub>hex</sub> || = || [[4 (number)|4<sub>dec</sub>]] ||= || 4<sub>oct</sub> ||style="background:black; width:2px" | || style="background:white;width:24px"|0|| style="background:red;width:24px"|1|| style="background:white;width:24px"|0|| style="background:white;width:24px"|0||style="background:black; width:2px" | |-style="background:#FDC888; color:black; height:24px" | style="background:black; width:2px" | || '''5'''<sub>hex</sub> || = || [[5 (number)|5<sub>dec</sub>]] || = || 5<sub>oct</sub> ||style="background:black; width:2px" | || style="background:white;width:24px"|0|| style="background:red;width:24px"|1|| style="background:white;width:24px"|0|| style="background:red;width:24px"|1||style="background:black; width:2px" | |-style="background:#FEE978; color:black; height:24px" | style="background:black; width:2px" | || '''6'''<sub>hex</sub> || = || [[6 (number)|6<sub>dec</sub>]] ||= || 6<sub>oct</sub> ||style="background:black; width:2px" | || style="background:white;width:24px"|0|| style="background:red;width:24px"|1|| style="background:red;width:24px"|1|| style="background:white;width:24px"|0||style="background:black; width:2px" | |-style="background:#E3EDE9; color:black; height:24px" | style="background:black; width:2px" | ||'''7'''<sub>hex</sub> || = || [[7 (number)|7<sub>dec</sub>]] || = || 7<sub>oct</sub> ||style="background:black; width:2px" | || style="background:white;width:24px"|0|| style="background:red;width:24px"|1|| style="background:red;width:24px"|1|| style="background:red;width:24px"|1||style="background:black; width:2px" | |- style="background:black; height:2px" | style="background:black; width:2px" | || || || || || || || || || || || |-style="background:#E3EDE9; color:black; height:24px" | style="background:black; width:2px" | || '''8'''<sub>hex</sub> || = || [[8 (number)|8<sub>dec</sub>]] || = || 10<sub>oct</sub> ||style="background:black; width:2px" | || style="background:red;width:24px"|1|| style="background:white;width:24px"|0|| style="background:white;width:24px"|0|| style="background:white;width:24px"|0||style="background:black; width:2px" | |-style="background:#FEE978; color:black; height:24px" | style="background:black; width:2px" | || '''9'''<sub>hex</sub> || = || [[9 (number)|9<sub>dec</sub>]] ||= || 11<sub>oct</sub> ||style="background:black; width:2px" | || style="background:red;width:24px"|1|| style="background:white;width:24px"|0|| style="background:white;width:24px"|0|| style="background:red;width:24px"|1||style="background:black; width:2px" | |-style="background:#FDC888; color:black; height:24px" | style="background:black; width:2px" | || '''A'''<sub>hex</sub>|| = || [[10 (number)|10<sub>dec</sub>]] || = || 12<sub>oct</sub> ||style="background:black; width:2px" | || style="background:red;width:24px"|1|| style="background:white;width:24px"|0|| style="background:red;width:24px"|1|| style="background:white;width:24px"|0||style="background:black; width:2px" | |-style="background:#E3EDE9; color:black; height:24px" | style="background:black; width:2px" | || '''B'''<sub>hex</sub>|| = || [[11 (number)|11<sub>dec</sub>]] || = || 13<sub>oct</sub> ||style="background:black; width:2px" | || style="background:red;width:24px"|1|| style="background:white;width:24px"|0|| style="background:red;width:24px"|1|| style="background:red;width:24px"|1||style="background:black; width:2px" | |- style="background:black; height:2px" | style="background:black; width:2px" | || || || || || || || || || || || |-style="background:#FDC888; color:black; height:24px" | style="background:black; width:2px" | || '''C'''<sub>hex</sub>|| = || [[12 (number)|12<sub>dec</sub>]] || = || 14<sub>oct</sub> ||style="background:black; width:2px" | || style="background:red;width:24px"|1|| style="background:red;width:24px"|1|| style="background:white;width:24px"|0|| style="background:white;width:24px"|0||style="background:black; width:2px" | |-style="background:#E3EDE9; color:black; height:24px" | style="background:black; width:2px" | || '''D'''<sub>hex</sub>|| = || [[13 (number)|13<sub>dec</sub>]] ||= || 15<sub>oct</sub> ||style="background:black; width:2px" | || style="background:red;width:24px"|1|| style="background:red;width:24px"|1|| style="background:white;width:24px"|0|| style="background:red;width:24px"|1||style="background:black; width:2px" | |-style="background:#E3EDE9; color:black; height:24px" | style="background:black; width:2px" | || '''E'''<sub>hex</sub>|| = || [[14 (number)|14<sub>dec</sub>]] ||= || 16<sub>oct</sub> ||style="background:black; width:2px" | || style="background:red;width:24px"|1|| style="background:red;width:24px"|1|| style="background:red;width:24px"|1|| style="background:white;width:24px"|0||style="background:black; width:2px" | |-style="background:#F6A07C; color:black; height:24px" | style="background:black; width:2px" | || '''F'''<sub>hex</sub>|| = || [[15 (number)|15<sub>dec</sub>]] || = || 17<sub>oct</sub> ||style="background:black; width:2px" | || style="background:red;width:24px"|1|| style="background:red;width:24px"|1|| style="background:red;width:24px"|1|| style="background:red;width:24px"|1||style="background:black; width:2px" | |- style="background:black; height:2px" | style="background:black; width:2px" | || || || || || || || || || || || |} </td> </tr> </table> In digital computing, hexadecimal is primarily used to represent [[byte]]s. Attempts to represent the 256 possible byte values by other means have led to problems. Directly representing each possible byte value with a single character representation runs into ''unprintable'' [[control character]]s in the [[ASCII]] character set. Even if a standard set of printable characters were devised for every byte value, neither users nor input hardware are equipped to handle 256 unique characters. Most hex [[editing]] software displays each byte as a single character, but unprintable characters are usually substituted with period or blank. In [[URL]]s, all characters ''can'' be coded using hexadecimal.<ref>See [[Request for Comments|RFC]] 3986 at RFC 3986.</ref> Each 2-digit (1 byte) hexadecimal sequence is preceded by a [[percent]] sign. For example, the URL <code>http://en.wikipedia.org/wiki/Main%20Page</code> substitutes a space (which is not allowed in URLs) with the hex code for a space (<code>%20</code>). ==Representing hexadecimal== In situations where there is no context, a hexadecimal number might be ambiguous and confused with numbers expressed in other bases. There are several conventions for unambiguously expressing values. In mathematics, a subscript is often used on each number explicitly giving the base: 159<sub>10</sub> is decimal 159; 159<sub>16</sub> is hexadecimal 159 which is equal to 345<sub>10</sub>. Some authors prefer a text subscript, such as 159<sub>decimal</sub> and 159<sub>hex</sub>. In linear text systems, such as those used in most computer programming environments, a variety of methods have arisen:<!-- *** These are ordered from most likely to be encountered by lay people *** to least likely to be encountered by lay people **** --> * In [[URL]]s, character codes are written as hexadecimal pairs prefixed with <code>%</code>: <code><nowiki>http://www.example.com/name%20with%20spaces</nowiki></code> where <code>%20</code> is the space (blank) character, code 20 hex, or 32 decimal. * In [[XML]] and [[XHTML]], characters can be expressed as hexadecimal using the notation <code>&amp;#F87A;</code>. Color references are expressed in hex prefixed with <code>#</code>: <code>#FFFFFF</code> which gives white.<ref> {{cite web | url = http://www.web-colors-explained.com/hex.php | title = Hexadecimal web colors explained }} </ref> * The [[C (programming language)|C programming language]] (and its syntactical descendants<ref>Some of C's syntactical descendants are [[C++]], [[C Sharp programming language|C#]], [[Java (programming language)|Java]], [[JavaScript]], and [[Windows PowerShell]]</ref>) use the prefix <code>0x</code>: <code>0x5A3</code> Character and string constants may express character codes in hexadecimal with the prefix <code>\x</code> followed by two hex digits: <code>'\x1B'</code> (specifies the [[Escape character|Esc]] control character), <code>"\x1B[0m\x1B[25;1H"</code> is a string containing 11 characters (not including an implied trailing NUL).<ref> The string <code>"\x1B[0m\x1B[25;1H"</code> specifies the characters: <tt>Esc [ 0 m Esc [ 2 5 ; 1 H</tt>. This expresses the escape sequences used to reset the character set and color then move the cursor to line 25 in an [[ANSI escape code|ANSI terminal]].</ref> To output a value as hexadecimal with the [[printf]] function family, the format conversion code <code>%X</code> or <code>%x</code> is used. * In the [[Unicode]] standard, a character value is represented with <code>U+<code> followed by the hex value: <code>U+20AC</code> is the [[Euro sign]] (€). * [[MIME]] (e-mail extensions) [[quoted-printable]] characters by code inside a <code>text/plain MIME-part</code> body prefix non-printable ASCII characters with an ''equal to'' sign <code>=</code>, as in <code>Espa=D1a</code> to send "España" (Spain). * In Intel-derived [[assembly language]]s, hexadecimal is indicated with a suffixed <tt>H</tt> or <tt>h</tt>: <code>FFh</code> or <code>0A3CH</code>. Some implementations require a leading zero when the first character is not a digit: <code>0FFh</code> * Other assembly languages ([[MOS Technology 6502|6502]], [[AT&T]], [[Motorola]]), [[Pascal (programming language)|Pascal]], and some versions of [[BASIC programming language|BASIC]] ([[Commodore BASIC|Commodore]]) and [[Forth (programming language)|Forth]] use <code>$</code> as a prefix: <code>$5A3</code>. * Some assembly languages (Microchip) use the notation <code>H'ABCD'</code> (for ABCD<sub>16</sub>). * [[*nix]] (UNIX and related) shells use an escape character form <code>\x0FF</code> in expressions and <code>0xFF</code> for constants. * [[Ada (programming language)|Ada]] and [[VHDL]] enclose hexadecimal numerals in based "numeric quotes": <code>16#5A3#</code> * [[Verilog]] represents hexadecimal constants in the form <code>8'hFF</code>, where 8 is the number of bits in the value and FF is the hexadecimal constant. * [[Modula 2]] and some other languages use # as a prefix: <code>#01AF</code> * The [[Smalltalk]] programming language uses the prefix <code>16r</code>: <code>16r6EF7</code> * [[Postscript programming language|Postscript]] indicates hex with prefix <code>16#</code>: <code>16#ABCD</code>. Binary data (such as image [[pixel]]s) can be expressed as unprefixed consecutive hexadecimal pairs: <code>AA213FD51B3801043FBC</code>... * [[Common Lisp]] use the prefixes <code>#x</code> and <code>#16r</code>. * [[QBasic]] and [[Visual Basic]], prefix hexadecimal numerals with <code>&amp;H</code>: <code>&amp;H5A3</code> * [[BBC BASIC]] and [[Locomotive_BASIC]] use <code>&amp;</code> for hex.<ref> BBC BASIC is not portable to Microsoft BASIC since the latter takes <code>&amp;</code> to prefix [[octal]] values.</ref> * [[TI-89]] and 92 series uses <code>0h</code>: <code>0hA3</code> * Notations such as <code>X'5A3'</code> are sometimes seen, such as in [[PL/I]]. This is the most common format for hexadecimal on IBM mainframes ([[zSeries]]) and minicomputers ([[iSeries]]) running 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. * [[Donald Knuth]] introduced the use of particular typeface to represent a particular radix in his book ''The TeXbook''.<ref> Donald E. Knuth. ''The TeXbook'' ([[Computers and Typesetting]], Volume A). Reading, Massachusetts: Addison-Wesley, 1984. ISBN 0-201-13448-9. The [http://www.ctan.org/tex-archive/systems/knuth/tex/texbook.tex source code of the book in TeX] (and a needed set of macros [ftp://tug.ctan.org/pub/tex-archive/systems/knuth/lib/manmac.tex]) is available online on [[CTAN]].</ref> There, hexadecimal representations are written in a typewriter typeface: <tt>5A3</tt> There is no universal convention to use lowercase or uppercase for the letter digits, and each is prevalent or preferred by particular environments by community standards or convention. [[Image:Bruce Martin hexadecimal notation proposal.png|thumb|Bruce A. Martin's hexadecimal notation proposal]] The choice of the letters ''A'' through ''F'' to represent the digits above nine was not universal in the early history of computers. During the 1950s, some installations favored using the digits 0 through 5 with a [[macron]] character ("¯") to indicate the values 10-15. Users of [[Bendix G-15]] computers used the letters ''U'' through ''Z''. [[Bruce A. Martin]] of [[Brookhaven National Laboratory]] considered the choice of A-F "ridiculous" and in 1968 proposed in a letter to the editor of the [[Association for Computing Machinery|ACM]] an entirely new set of symbols based on the bit locations, which did not gain much acceptance<!-- if any -->.<ref>''Letters to the editor: On binary notation'', Bruce A. Martin, Associated Universities Inc., Communications of the ACM, Volume 11, Issue 10 (October 1968) Page: 658 {{DOI|10.1145/364096.364107}}</ref> [[Image:Hexadecimal multiplication table.svg|right|thumb|A hexadecimal [[multiplication table]]]] ==Verbal representations== Not only are there no digits to represent the quantities from ten to fifteen—so letters are used as a substitute—but most [[Western European]] languages also lack a nomenclature to name hexadecimal numbers. "Thirteen" and "fourteen" are [[decimal]]-based, and even though English has names for several non-decimal powers: ''[[pair]]'' for the first [[binary numeral system|binary]] power; ''[[Twenty|score]]'' for the first [[vigesimal]] power; ''[[dozen]]'', ''[[Gross (unit)|gross]]'', and ''[[great gross]]'' for the first three [[duodecimal]] powers. However, no English name describes the hexadecimal powers (corresponding to the decimal values 16, 256, 4096, 65536, ...). Some people read hexadecimal numbers digit by digit like a phone number: 4DA is "four-dee-aye". However, the letter 'A' sounds similar to eight, 'C' sounds similar to three, and 'D' can easily be mistaken for the 'ty' suffix: Is it 4D or forty? Other people avoid confusion by using the [[NATO phonetic alphabet]]: 4DA is "four-delta-alpha". Similarly, some use the [[Joint Army/Navy Phonetic Alphabet]] ("four-dog-able"), or a similar ad hoc system. ==Signs== The hexadecimal system can express negative numbers the same way as in decimal: –2A to represent –42 and so on. However, some prefer instead to express the exact bit patterns used in the [[CPU|processor]] and consider hexadecimal values best handled as unsigned values. This way, the negative number –42 can be written as FFFF&nbsp;FFD6 in a 32-bit [[Processor register|CPU register]], as C228&nbsp;0000 in a 32-bit [[Floating point unit|FPU]] register or C045&nbsp;0000&nbsp;0000&nbsp;0000 in a 64-bit FPU register. ==Fractions== As with other numeral systems, the hexadecimal system can be used to represent [[rational number]]s, although [[Recurring decimal|recurring digits]] are common since sixteen (10h) has only a single prime factor (two): {| Border=0 cellspacing=0 cellpadding=3 |align=right| &nbsp;&nbsp;&nbsp;&nbsp;'''{{Fraction|1|2}}''' ||<center> '''=''' || '''0.8''' |align=right| &nbsp;&nbsp;&nbsp;&nbsp;{{Fraction|1|6}} ||<center> = || 0.2<font style="text-decoration: overline">A</font>AAAAAAA... |align=right| &nbsp;&nbsp;&nbsp;&nbsp;{{Fraction|1|A}} ||<center> = || 0.1<font style="text-decoration: overline">9</font>99999999... |align=right| &nbsp;&nbsp;&nbsp;&nbsp;{{Fraction|1|E}} ||<center> = || 0.1<font style="text-decoration: overline">249</font>249249... |- |align=right| &nbsp;&nbsp;&nbsp;&nbsp;{{Fraction|1|3}} ||<center> = || 0.<font style="text-decoration: overline">5</font>555555555... |align=right| &nbsp;&nbsp;&nbsp;&nbsp;{{Fraction|1|7}} ||<center> = || 0.<font style="text-decoration: overline">249</font>2492492... |align=right| &nbsp;&nbsp;&nbsp;&nbsp;{{Fraction|1|B}} ||<center> = || 0.<font style="text-decoration: overline">1745D</font>1745D... |align=right| &nbsp;&nbsp;&nbsp;&nbsp;{{Fraction|1|F}} ||<center> = || 0.<font style="text-decoration: overline">1</font>111111111... |- |align=right| &nbsp;&nbsp;&nbsp;&nbsp;'''{{Fraction|1|4}}''' ||<center> '''=''' || '''0.4''' |align=right| &nbsp;&nbsp;&nbsp;&nbsp;'''{{Fraction|1|8}}''' ||<center> '''=''' || '''0.2''' |align=right| &nbsp;&nbsp;&nbsp;&nbsp;{{Fraction|1|C}} ||<center> = || 0.1<font style="text-decoration: overline">5</font>55555555... |align=right| &nbsp;&nbsp;&nbsp;&nbsp;'''{{Fraction|1|10}}''' ||<center> '''=''' || '''0.1''' |- |align=right| &nbsp;&nbsp;&nbsp;&nbsp;{{Fraction|1|5}} ||<center> = || 0.<font style="text-decoration: overline">3</font>333333333... |align=right| &nbsp;&nbsp;&nbsp;&nbsp;{{Fraction|1|9}} ||<center> = || 0.<font style="text-decoration: overline">1C7</font>1C71C71... |align=right| &nbsp;&nbsp;&nbsp;&nbsp;{{Fraction|1|D}} ||<center> = || 0.<font style="text-decoration: overline">13B</font>13B13B1... |align=right| &nbsp;&nbsp;&nbsp;&nbsp;{{Fraction|1|11}} ||<center> = || 0.<font style="text-decoration: overline">0F</font>0F0F0F0F... |} 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: Counting in base 3 is 0, 1, 2, 10 (three). Thus, whether dividing one by two for [[binary]] or dividing one by sixteen for hexadecimal, both of these fractions are written as <code>0.1</code>. 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 number]]s (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 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 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: That is, decimal 0.1 corresponds to the infinite recurring representation 0.199999999999... in hexadecimal. However, hexadecimal is more efficient than bases 12 and 60 for 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). ==Binary translation== Most computers manipulate binary data, but it is difficult for humans to work with the 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 (4<sub>10</sub>). This example converts 1111<sub>2</sub> to base ten. Since each [[Positional notation|position]] in a binary numeral can contain either a 1 or 0, its value may be easily determined by its position from the right: *0001<sub>2</sub> = 1<sub>10</sub> *0010<sub>2</sub> = 2<sub>10</sub> *0100<sub>2</sub> = 4<sub>10</sub> *1000<sub>2</sub> = 8<sub>10</sub> Therefore: <table> <tr> <td>1111<sub>2</sub></td> <td> = 8<sub>10</sub> + 4<sub>10</sub> + 2<sub>10</sub> + 1<sub>10</sub></td> </tr> <tr> <td>&nbsp;</td> <td> = 15<sub>10</sub></td> </tr> </table> With surprisingly little practice, mapping 1111<sub>2</sub> to F<sub>16</sub> in one step becomes easy: see table in [[Hexadecimal#Uses|Uses]]. 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. <table> <td>01011110101101010010<sub>2</sub></td> <td> = 262144<sub>10</sub> + 65536<sub>10</sub> + 32768<sub>10</sub> + 16384<sub>10</sub> + 8192<sub>10</sub> + 2048<sub>10</sub> + 512<sub>10</sub> + 256<sub>10</sub> + 64<sub>10</sub> + 16<sub>10</sub> + 2<sub>10</sub></td> </tr> <tr> <td>&nbsp;</td> <td> = 387922<sub>10</sub></td> </tr> </table> Compare this to the conversion to hexadecimal, where each group of four digits can be considered independently, and converted directly: <table> <tr> <td>01011110101101010010<sub>2</sub></td> <td> = </td> <td>0101<sub>&nbsp;</sub></td> <td>1110<sub>&nbsp;</sub></td> <td>1011<sub>&nbsp;</sub></td> <td>0101<sub>&nbsp;</sub></td> <td>0010<sub>2</sub></td> </tr> <tr> <td>&nbsp;</td> <td> = </td> <td align="center">5</td> <td align="center">E</td> <td align="center">B</td> <td align="center">5</td> <td align="center">2<sub>16</sub></td> </tr> <tr> <td>&nbsp;</td> <td> = </td> <td colspan="5">5EB52<sub>16</sub></td> </tr> </table> The conversion from hexadecimal to binary is equally direct. The [[octal]] system can also be useful as a tool for people who need to deal directly with binary computer data. Octal represents data as three bits per character, rather than four. ==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<sub>i</sub>h<sub>i-1</sub>...h<sub>2</sub>h<sub>1</sub> be the hexadecimal digits representing the number. #i := 1 #h<sub>i</sub> := d mod 16 #d := (d-h<sub>i</sub>) / 16 #If d = 0 (return series h<sub>i</sub>) 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]]. <source lang="javascript"> 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); } </source> ===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 and then performing multiplication and addition to get the final representation. I.e. to convert the number B3AD to decimal one can split the conversion into D (13<sub>10</sub>), A (10<sub>10</sub>), 3 (3<sub>10</sub>) and B (11<sub>10</sub>) then get the final result by multiplying each decimal representation by 16<sup>p</sup>, where 'p' is the corresponding position from right to left, beginning with 0. In this case we have 13*(16<sup>0</sup>) + 10*(16<sup>1</sup>) + 3*(16<sup>2</sup>) + 11*(16<sup>3</sup>), which is equal 45997 in decimal system. ===Conversion via binary=== As most computers 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. ===Tools for conversion=== Most modern computer systems with [[graphical user interface]]s provide a built-in calculator utility, capable of performing conversions between various radixes, generally including hexadecimal. In [[Microsoft]] [[Microsoft Windows|Windows]], the [[Calculator (Windows)|Calculator]] utility can be set to [[scientific calculator]] mode, which allows conversions between radix 16 (hexadecimal), 10 (decimal), 8 ([[octal]]) and 2 ([[Binary numeral system|binary]]); 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. ==Cultural== ===Etymology=== It was IBM that decided on the prefix of "hexa" rather than the proper Latin prefix of "sexa".{{Fact|date=June 2008}} The word "hexadecimal" is strange in that ''hexa'' is derived from the [[Greek language|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é,{{Fact|date=March 2008}} 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".)<ref> Knuth, Donald. (1969). ''Donald Knuth, in The Art of Computer Programming, Volume 2''. ISBN 0-201-03802-1. (Chapter 17.) </ref> Schwartzman notes that the expected purely Latin form would be "sexadecimal", but then computer hackers would be tempted to shorten the word to "sex".<ref> Schwartzman, S. (1994). ''The Words of Mathematics: an etymological dictionary of mathematical terms used in English''. ISBN 0-88385-511-9. </ref> Incidentally, the [[Etymology|etymologically]] proper [[Greek language|Greek]] term would be ''hexadecadic'' (although in [[Modern Greek]] ''deca-hexadic (δεκαεξαδικός)'' is more commonly used). ===Common patterns and 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". 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 language|assembly]] syntax, this is sometimes the case. "[[Hexspeak]]" may be the forerunner of the modern web parlance of "[[Leet|1337speak]]" An example is the [[magic number (programming)|magic number]] in FAT Mach-O files and [[Java Platform|java]] [[class file]] structure, which is "<code>CAFEBABE</code>". Single-architecture Mach-O files have the magic number "<code>FEEDFACE</code>" 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|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. Obviously, DEAD should not be written in all-caps (as in the proceeding) as it makes dead stand out, thus ruining the riddle. Microsoft Windows XP clears its locked index.dat files with the hex codes: "0BADF00D". Two common bit patterns often employed to test hardware are <code>01010101</code> and <code>10101010</code> (their corresponding hex values are 55h and AAh, respectively). The reason for their use is to alternate between ''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, <code>0xAA55</code> which on [[endianness|little-endian]] systems is 55h followed by AAh, must at the end of a valid [[master boot record|Master Boot Record]]). ===Primary numeral system=== There have been occasional attempts to promote hexadecimal as the preferred numeral system. These attempts usually propose pronunciation and/or symbology. Sometimes the proposal unifies standard measures so that they are multiples of 16.<ref> {{cite web | url = http://www.intuitor.com/hex/ | title = Intuitor Hex Headquarters }} </ref><ref> {{cite web | url = http://std.dkuug.dk/jtc1/sc2/wg2/docs/n2677 | title = A proposal for addition of the six Hexadecimal digits (A-F) to Unicode }} </ref><ref name="nystrom"> {{cite book | last=Nystrom | first=John William | title=Project of a New System of Arithmetic, Weight, Measure and Coins: Proposed to be called the Tonal System, with Sixteen to the Base |year=1862 | url=http://books.google.com/books?id=aNYGAAAAYAAJ | location=Philadelphia}}</ref> An example of unifying standard measures is [[Hexadecimal time]] which subdivides a day by 16 so that there are 16 "hexhours" in a day.<ref name="nystrom" /> ==See also== <div style="-moz-column-count:2; column-count:2;"> *[[Base 32]] *[[Base 64]] *[[Web colours]] *[[Hex editor]] *[[Hexadecimal time]] *[[Hexspeak]] *[[Nibble]] &mdash; one hexadecimal digit can exactly represent one "nibble" *[[Numeral system]] &mdash; a list of other base systems *[[Binary numeral system]] *[[HTML]] *[[Bubble Babble]] </div> ==References== {{reflist|2}} ==External links== ===Hex conversion utilities or pages=== *[http://netzreport.googlepages.com/online_converter_for_dec_hex.html Online Converter] for Decimal/Hexadecimal Numerals ([[JavaScript]], [[GPL]]) *[http://textop.us/Text-Convert/Hexadecimal Online ASCII/Hexadecimal converter (PHP)] *[http://www.defproc.co.uk/toys/hex.php Hex/ASCII 'translation' service] *[http://leetkey.mozdev.org Leet Key], a Firefox extension that supports ASCII/Hex conversions and typing *[http://hexday.com Hexday], a web based social network built around hex color choices [[Category:Computer arithmetic]] [[Category:Positional numeral systems| 16]] <!--Please note that the blank after | is intentional--> [[ar:نظام عد سداسي عشر]] [[bs:Heksadecimalni sistem]] [[br:Diazez c'hwezekred]] [[bg:Шестнадесетична бройна система]] [[ca:Sistema hexadecimal]] [[cs:Hexadecimální soustava]] [[da:Hexadecimale talsystem]] [[de:Hexadezimalsystem]] [[el:Δεκαεξαδικό σύστημα αρίθμησης]] [[es:Sistema hexadecimal]] [[eo:Deksesuma sistemo]] [[eu:Zenbaki-sistema hamaseitar]] [[fa:هگزادسیمال]] [[fr:Système hexadécimal]] [[gl:Código hexadecimal]] [[ko:십육진법]] [[hr:Heksadekadski brojevni sustav]] [[id:Heksadesimal]] [[is:Sextánundakerfi]] [[it:Sistema numerico esadecimale]] [[he:בסיס הקסדצימלי]] [[ht:Sistèm ekzadesimal]] [[hu:Tizenhatos számrendszer]] [[ms:Nombor perenambelasan]] [[nl:Hexadecimaal]] [[ja:十六進法]] [[no:Sekstentallsystemet]] [[nn:Sekstentalssystemet]] [[pl:Szesnastkowy system liczbowy]] [[pt:Sistema hexadecimal]] [[ro:Sistem hexazecimal]] [[ru:Шестнадцатеричная система счисления]] [[simple:Hexadecimal numeral system]] [[sk:Šestnástková sústava]] [[sl:Šestnajstiški številski sistem]] [[sr:Хексадецимални систем]] [[sh:Heksadecimalni sistem]] [[fi:Heksadesimaalijärjestelmä]] [[sv:Hexadecimala talsystemet]] [[th:เลขฐานสิบหก]] [[vi:Hệ thập lục phân]] [[tr:Heksadesimal]] [[uk:Шістнадцяткова система числення]] [[yi:העקס]] [[zh:十六进制]]