Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Hexadecimal
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Conversion== ===Binary conversion=== [[File:Hewlett-Packard Model HP-16C Programmable RPN Calculator, HP's First and Only Calculator esp. for Programmers, built 1982-1989 (edited to rectangular, V2).jpg|thumb|The programmable [[Reverse Polish notation|RPN]]-calculator [[HP-16C|HP-16C Computer Scientist]] from 1982 was designed for programmers. One of its key features was the conversion between different numeral systems (note hex number in display).]] 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 (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 a 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: {| |- | 1111<sub>2</sub>|| = 8<sub>10</sub> + 4<sub>10</sub> + 2<sub>10</sub> + 1<sub>10</sub> |- | || = 15<sub>10</sub> |} With little practice, mapping 1111<sub>2</sub> to F<sub>16</sub> in one step becomes easy (see table in [[#Written representation|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.<ref name=Mano-Ciletti/> This example shows the conversion of a binary number to decimal, mapping each digit to the decimal value, and adding the results. {| | (1001011100)<sub>2</sub>|| = 512<sub>10</sub> + 64<sub>10</sub> + 16<sub>10</sub> + 8<sub>10</sub> + 4<sub>10</sub> |- | || = 604<sub>10</sub> |} Compare this to the conversion to hexadecimal, where each group of four digits can be considered independently and converted directly: {| |- | (1001011100)<sub>2</sub>|| = ||0010<sub> </sub>||0101<sub> </sub>||1100<sub>2</sub> |- | || = || align="center" |2|| align="center" |5||align="center" |C<sub>16</sub> |- | || = || colspan="5" |25C<sub>16</sub> |} The conversion from hexadecimal to binary is equally direct.<ref name=Mano-Ciletti>{{cite book|title=Digital Design β With an Introduction to the Verilog HDL|edition=Fifth|last1=Mano|first1=M. Morris|last2=Ciletti|first2=Michael D.|publisher=[[Pearson Education]]|date=2013|pages=6, 8β10|isbn=978-0-13-277420-8}}</ref> ===Other simple conversions=== Although [[Quaternary numeral system|quaternary]] (base 4) 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 2 5 C<sub>16</sub> = 02 11 30<sub>4</sub>. The [[octal]] (base 8) 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 (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]]. <syntaxhighlight lang="javascript"> function toHex(d) { var r = d % 16; if (d - r == 0) { return toChar(r); } return toHex((d - r) / 16) + toChar(r); } function toChar(n) { const alpha = "0123456789ABCDEF"; return alpha.charAt(n); } </syntaxhighlight> ===Conversion through addition and multiplication=== [[Image:Hexadecimal multiplication table.svg|right|thumb|A hexadecimal [[multiplication table]]]] 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 (11<sub>10</sub>), 3 (3<sub>10</sub>), A (10<sub>10</sub>) and D (13<sub>10</sub>), and then get the final result by multiplying each decimal representation by 16<sup>''p''</sup> (''p'' being the corresponding hex digit position, counting from right to left, beginning with 0). In this case, we have that: {{math|B3AD {{=}} (11βΓβ16<sup>3</sup>) + (3βΓβ16<sup>2</sup>) + (10βΓβ16<sup>1</sup>) + (13βΓβ16<sup>0</sup>)}} which is 45997 in base 10. ===Tools for conversion=== Many computer systems provide a calculator utility capable of performing conversions between the various radices frequently including hexadecimal. In [[Microsoft Windows]], the [[Calculator (Windows)|Calculator]], on its Programmer mode, allows conversions between hexadecimal and other common programming bases.
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Hexadecimal
(section)
Add topic