{"id":1146,"date":"2017-10-18T23:51:38","date_gmt":"2017-10-19T03:51:38","guid":{"rendered":"http:\/\/www.circuitcrush.com\/?p=1146"},"modified":"2021-06-30T17:55:48","modified_gmt":"2021-06-30T21:55:48","slug":"hexadecimal-numbers-tutorial","status":"publish","type":"post","link":"https:\/\/www.circuitcrush.com\/hexadecimal-numbers-tutorial\/","title":{"rendered":"Breaking the Hex: Understanding Hexadecimal Numbers"},"content":{"rendered":"<p>Just as understanding binary numbers is important, so is understanding hexadecimal or hex numbers. Before we go into any detail about hexadecimal numbers, I strongly suggest you read the <a href=\"http:\/\/www.circuitcrush.com\/binary-numbers-basics\/\" target=\"_blank\" rel=\"noopener\">post on binary numbers<\/a>.<\/p>\n<p>In it, you\u2019ll learn why binary numbers are used in digital systems. This will help you understand why hex numbers also often appear in digital systems. You\u2019ll also get a quick review of the decimal system (and number systems in general). Finally, you\u2019ll learn how to read binary, which will help you understand how to translate from binary to hex.<\/p>\n<p>As I said before, if you\u2019re new to digital electronics, it\u2019s a must read.<\/p>\n<p>And if you\u2019re not so new it\u2019s a good review.<\/p>\n<p>Let\u2019s break the hex and learn about hexadecimal numbers!<\/p>\n<p><!--more--><\/p>\n<h1><strong>Hexadecimal Number Basics<\/strong><\/h1>\n<p>After binary numbers, hex numbers or base 16 numbers are the most important in digital applications.<\/p>\n<p>The word hexadecimal comes from the Greek root <em>hex<\/em> \u2013 which means six, and the Latin root <em>deci<\/em> \u2013 meaning ten. Hence, the word hexadecimal refers to sixteen.<\/p>\n<p>Hexadecimal numbers use a base 16 system which means that the positional multipliers in the hex system are powers of 16: 16<sup>0<\/sup> = 1, 16<sup>1<\/sup> = 16, 16<sup>2<\/sup> = 256 and so on. This is just like the decimal system were all familiar with, only instead of powers of 10 we\u2019re dealing with powers of 16.<\/p>\n<p>The chart below will come in handy for figuring out various powers of 16. My, how quickly they grow!<\/p>\n<p>Note that MSD stands for most significant digit and LSD stands for least significant digit. Sorry for the disappointment, LSD is not a drug &#8212; at least not in this post.<\/p>\n<p><img decoding=\"async\" class=\"size-full wp-image-1147 alignnone\" src=\"http:\/\/www.circuitcrush.com\/wp-content\/uploads\/Hexadecimal-Powers-of-16.jpg\" alt=\"Powers of 16\" width=\"626\" height=\"68\" srcset=\"https:\/\/www.circuitcrush.com\/wp-content\/uploads\/Hexadecimal-Powers-of-16.jpg 626w, https:\/\/www.circuitcrush.com\/wp-content\/uploads\/Hexadecimal-Powers-of-16-600x65.jpg 600w, https:\/\/www.circuitcrush.com\/wp-content\/uploads\/Hexadecimal-Powers-of-16-150x16.jpg 150w, https:\/\/www.circuitcrush.com\/wp-content\/uploads\/Hexadecimal-Powers-of-16-300x33.jpg 300w\" sizes=\"(max-width: 626px) 100vw, 626px\" \/><\/p>\n<p><strong><em>Figure 1: various powers of 16.<\/em><\/strong><\/p>\n<p>The first 9 digits in the hexadecimal system are the same digits 0-9 that you\u2019re familiar with. However, the next 6 are represent by the first few letters of the alphabet: A, B, C, D, E, and F.<\/p>\n<p>The first 16 digits on the hex system are given in the chart below along with their decimal and binary equivalents.<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\" wp-image-1148 alignnone\" src=\"http:\/\/www.circuitcrush.com\/wp-content\/uploads\/Hexadecimal-Numbers.jpg\" alt=\"Hexadecimal Numbers\" width=\"578\" height=\"730\" srcset=\"https:\/\/www.circuitcrush.com\/wp-content\/uploads\/Hexadecimal-Numbers.jpg 703w, https:\/\/www.circuitcrush.com\/wp-content\/uploads\/Hexadecimal-Numbers-600x758.jpg 600w, https:\/\/www.circuitcrush.com\/wp-content\/uploads\/Hexadecimal-Numbers-119x150.jpg 119w, https:\/\/www.circuitcrush.com\/wp-content\/uploads\/Hexadecimal-Numbers-238x300.jpg 238w\" sizes=\"(max-width: 578px) 100vw, 578px\" \/><\/p>\n<p><strong><em>Figure 2: hex numbers and their decimal and binary equivalents.<\/em><\/strong><\/p>\n<p>If you\u2019re not too familiar with the hexadecimal system, the use of letters to represent numbers may seem odd at first. Keep in mind though that any number system is only a set of sequential symbols. The form of the symbols is unimportant if you understand what they mean.<\/p>\n<p>Remember also, that regardless of the number system, each digit must be represented by a single symbol.<\/p>\n<h3 style=\"text-align: center;\">Become the Maker you were born to be. Try <a href=\"https:\/\/learnarduinonow.com\">Arduino Academy<\/a> for FREE!<\/h3>\n<p><img decoding=\"async\" class=\"aligncenter  wp-image-4238\" src=\"https:\/\/www.circuitcrush.com\/wp-content\/uploads\/FB_Cover2.png\" alt=\"\" width=\"436\" height=\"166\" srcset=\"https:\/\/www.circuitcrush.com\/wp-content\/uploads\/FB_Cover2.png 828w, https:\/\/www.circuitcrush.com\/wp-content\/uploads\/FB_Cover2-300x114.png 300w, https:\/\/www.circuitcrush.com\/wp-content\/uploads\/FB_Cover2-150x57.png 150w, https:\/\/www.circuitcrush.com\/wp-content\/uploads\/FB_Cover2-768x292.png 768w\" sizes=\"(max-width: 436px) 100vw, 436px\" \/><\/p>\n<p>If we were to use the symbols that make the number ten in decimal (rather than A) when writing numbers in hex, it would be hard to tell whether we were referring to 10 in decimal, or 10 in hex which equals 16 in decimal.<\/p>\n<p>There is also perhaps an even more important reason for this, which we\u2019ll disclose in the next section.<\/p>\n<h1><strong>Why Use Hexadecimal Numbers?<\/strong><\/h1>\n<p>Right now, it may be easy to think something like <em>why bother with hex numbers? Base 16 is weird and seems complicated\u2026<\/em><\/p>\n<p>Or maybe you\u2019ve already figured out the virtues of using hexadecimal numbers. If not, you will in a minute.<\/p>\n<p>The translation from hex to binary in figure 2 suggest a very important reason hex numbers are popular.<\/p>\n<p>Hexadecimal numbers are <strong><em>way<\/em><\/strong> more compact than binary and more compact than decimal numbers. Hex numbers pack more information into fewer digits.<\/p>\n<p>Also, binary numbers are difficult to read and write because it is easy to drop or transpose a bit.<\/p>\n<p>Because of this, hexadecimal is widely used in computer and microprocessor applications.<\/p>\n<p>You\u2019ve undoubtedly been faced with a blue screen on your computer after a crash. In addition, you\u2019ll often see a cryptic message along with some hex numbers like 0xa56b170f.<\/p>\n<p>The hex numbers refer to memory locations and data. If the PC was to spit it out in binary, the whole screen would be filled with 1\u2019s and 0\u2019s.<\/p>\n<p>The prefix 0x tells us that the number is a hex number. The programming languages C and Java use this notation, but you may also see a hex number with the prefix 0h.<\/p>\n<p>Almost all computer data sizes are multiples of 4. Since base 16 is a power of 2 (like 4), converting from binary to hex and vice versa is trivial, as we\u2019ll see in a minute.<\/p>\n<h2><strong>Converting Binary Numbers to Hexadecimal Numbers<\/strong><\/h2>\n<p>As usual, the easiest way to convert a binary number to a hex number or vice versa is to use a calculator. However, you should be able to do it almost as fast after reading this section.<\/p>\n<p>You should know all the hexadecimal numbers from 0 to F (0 to 15).<\/p>\n<p>Figure 2 shows us that for every possible 4-bit binary number, there is a hex equivalent.<\/p>\n<p>To convert a binary number to a hex number, simply break the binary number into 4-bit groups, starting with the LSB (right-most bit). Then replace each 4-bit group with its hexadecimal equivalent.<\/p>\n<p>As you move to the left, you may notice that you have less than 4 binary digits left in the last group.<\/p>\n<p>No sweat. Just pad the last group with enough leading zeros to reach 4 bits, then convert to hex.<\/p>\n<p>Let\u2019s do a couple examples to illustrate this.<\/p>\n<p><strong><u>Ex.1 <\/u><\/strong><\/p>\n<p>Convert 1100101001010111 to hexadecimal.<\/p>\n<p><strong>Solution:<\/strong><\/p>\n<p>First, we break the binary number into groups of 4 bits:<\/p>\n<p>1100 1010 0101 0111<\/p>\n<p>Next, we simply find the hex equivalents of each of the groups of 4 binary digits, starting at the left:<\/p>\n<p>1100 1010 0101 0111<\/p>\n<p><strong>\u00a0\u00a0 |\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 |\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 |\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 |<\/strong><\/p>\n<p><strong>\u00a0 C\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 A\u00a0\u00a0\u00a0\u00a0 \u00a0 5\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 7<\/strong><\/p>\n<p>1100 = 12 in decimal which is C in hex. Following a similar approach, we get A for the next group, 5 for the next and finally 7 for the last group.<\/p>\n<p>Now we write CA57 or 0xCA57 so we know it\u2019s a hex number. Wham, bam, thank you ma\u2019am.<\/p>\n<p><strong><u>Ex. 2<\/u><\/strong><\/p>\n<p>Convert 11100010100110 to hex.<\/p>\n<p><strong>Solution:<\/strong><\/p>\n<p>Starting from the right (LSB), we break the binary number into groups of 4 bits:<\/p>\n<p>11 1000 1010 0110<\/p>\n<p>Since the group on the far left only has 2 digits, we pad it with 2 zeros:<\/p>\n<p>0011 1000 1010 0110<\/p>\n<p>Now, we take the hex equivalent of each of the 4 groups:<\/p>\n<p>0011 1000 1010 0110<\/p>\n<p><strong>\u00a0\u00a0 |\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 |\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 |\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 |<\/strong><\/p>\n<p><strong>\u00a0 3\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 8\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 A\u00a0\u00a0\u00a0\u00a0\u00a0 6<\/strong><\/p>\n<p>Now, we write 0x38A6.<\/p>\n<p>Note that while it\u2019s obvious that if a string of digits contains any of the letters A through F the string is a hex number, not all hex numbers will contain letters. Therefore, to avoid confusion, especially in this case, it\u2019s a good idea to use the 0x prefix when writing or typing hex numbers.<\/p>\n<p>The general algorithm for converting a binary number to a hexadecimal number is as follows:<\/p>\n<ol>\n<li>Group the digits of the binary number into sets of 4 digits (or bits). Add leading zeros to last group of 4 on left if necessary.<\/li>\n<li>Convert the binary numbers in each set of 4 into the equivalent hex digits.<\/li>\n<\/ol>\n<h2><strong>Converting Hexadecimal Numbers to Binary Numbers<\/strong><\/h2>\n<p>The examples above suggest that this is also easy and trivial.<\/p>\n<p>By simply doing the algorithm in reverse, we can convert a hexadecimal number to a binary number.<\/p>\n<p>Let\u2019s do a quick example.<\/p>\n<p><strong><u>Ex. 3<\/u><\/strong><\/p>\n<p>Convert 0xFF09 to binary.<\/p>\n<p><strong>Solution:<\/strong><\/p>\n<p>Starting at the far left, we write each hex digit in its equivalent binary format:<\/p>\n<p>F = 1111, F = 1111, 0 = 0000, 9 = 1001<\/p>\n<p>Stringing it all together, we get: 1111111100001001 or 1111 1111 0000 1001 if we break in up for easier reading.<\/p>\n<p>The general algorithm for converting a binary number to a hexadecimal number is as follows:<\/p>\n<ol>\n<li>Write each hex digit as a 4-bit binary number<\/li>\n<li>Remember that 0 in hex is 0000 in binary!<\/li>\n<\/ol>\n<h1><strong>Converting Decimal Numbers to Hex Numbers<br \/>\n<\/strong><\/h1>\n<p>Going from decimal to hex is a bit more complicated than going from binary to hex, but as we\u2019ll see it\u2019s similar to converting decimal to binary and not that hard to do.<\/p>\n<p>There are two ways to convert decimal numbers to hexadecimal: sum of powers of 16 and repeated division by 16. Let\u2019s talk about both.<\/p>\n<h2><strong>Sum of Powers of 16<br \/>\n<\/strong><\/h2>\n<p>Also called sum of weighted hex digits method, this method is good for simpler conversions (up to about 3 hex digits).<\/p>\n<p>For example, let\u2019s convert 35 to hexadecimal using this method.<\/p>\n<p>Upon inspection, we note that 35 = 32 + 3<\/p>\n<p>We also note that (2 x 16) = 32 and that (3 x 1) = 3.<\/p>\n<p>So, we have 2 sixteens (or 2 sixteens raised to the first power) and 3 ones (or 3 sixteens raised to the zeroth power).<\/p>\n<p>Therefore 32 in decimal = 0x23.<\/p>\n<p>Remember that any number raised to the zeroth power is one.<\/p>\n<h2><strong>Repeated division by 16<br \/>\n<\/strong><\/h2>\n<p>The method above can become unwieldy for larger numbers.<\/p>\n<p>Divide the decimal number by 16 and note the remainder. Be sure to write the remainder as a hexadecimal digit.<\/p>\n<p>Repeat this process until the quotient is 0. The last remainder is the most significant digit of the hex number.<\/p>\n<p><strong><u>Ex. 4<\/u><\/strong><\/p>\n<p>Suppose we want to convert 31,581 to hexadecimal.<\/p>\n<p>31,581 \/ 16 = 1973, remainder 13 (least significant digit, or LSD).<\/p>\n<p>1,973 \/ 16 = 123, remainder 5<\/p>\n<p>123 \/ 16 = 7, remainder 11<\/p>\n<p>7 \/ 16 = 0, remainder 7 (most significant digit, or MSD)<\/p>\n<p>Now we write all the remainders as hex digits: 13 = 0xD, 5 = 0x5, 11 = 0xB, 7 = 0x7<\/p>\n<p>Listing them in proper order (MSD on far left; LSD on far right) gives us:<\/p>\n<p>31,581 = 0x7B5D<\/p>\n<h1><strong>Converting Hex Numbers to Decimal Numbers<br \/>\n<\/strong><\/h1>\n<p>One way to do this is to first convert the hex number into binary, then convert into decimal.<\/p>\n<p>Another way to do this conversion is to multiply each digit by its power of 16 positional multiplier and add the products together.<\/p>\n<p>The example below helps illustrate this.<\/p>\n<p><strong><u>Ex. 5<\/u><\/strong><\/p>\n<p>Convert 0x7C6 to decimal.<\/p>\n<p><strong>Solution:<\/strong><\/p>\n<p>First, we note each hex digit\u2019s positional weight starting with the LSD: 6 has a weight of 16<sup>0<\/sup>, C has a weight of 16<sup>1<\/sup>, and 7 has a weight of 16<sup>2<\/sup>.<\/p>\n<p>Now we multiply each digit by its positional weight. We\u2019ll start with the MSD:<\/p>\n<p>7 x 16<sup>2<\/sup> = 7 x 256 = 1792<\/p>\n<p>C x 16<sup>1<\/sup> = 12 x 16 = 192<\/p>\n<p>6 x 16<sup>0<\/sup> = 6 x 1 = 6<\/p>\n<p>Next, we\u2019ll add up our results: 1792 + 192+ 6 = 1990<\/p>\n<h2><strong>Taking the \u201cHex\u201d Out of Hexadecimal Numbers<\/strong><\/h2>\n<p>So now we know how hexadecimal numbers relate to digital circuits and how to convert hex to binary and vice versa. We also know how to convert hexadecimal numbers to decimal numbers and the reverse.<\/p>\n<p>Since hex numbers are quite common in the world of computers and electronics, I\u2019m sure they\u2019ll come up again.<\/p>\n<p>Only now you\u2019ll know how to deal with them. And as a bonus, next time you experience the blue screen of death, you\u2019ll be able to translate those hex digits into the memory address(es) where the error(s) occurred.<\/p>\n<p>Comment and tell us what you prefer working with: hex or binary?<\/p>\n<p>Or you can comment and let me know what other topics you\u2019d like to hear about.<\/p>\n<p>Until next time,<\/p>\n<p>Keep geekin\u2019<\/p>\n<h2 style=\"text-align: center;\">Become the Maker you were born to be. Try <a href=\"https:\/\/learnarduinonow.com\">Arduino Academy<\/a> for FREE!<\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-4238\" src=\"https:\/\/www.circuitcrush.com\/wp-content\/uploads\/FB_Cover2.png\" alt=\"\" width=\"828\" height=\"315\" srcset=\"https:\/\/www.circuitcrush.com\/wp-content\/uploads\/FB_Cover2.png 828w, https:\/\/www.circuitcrush.com\/wp-content\/uploads\/FB_Cover2-300x114.png 300w, https:\/\/www.circuitcrush.com\/wp-content\/uploads\/FB_Cover2-150x57.png 150w, https:\/\/www.circuitcrush.com\/wp-content\/uploads\/FB_Cover2-768x292.png 768w\" sizes=\"(max-width: 828px) 100vw, 828px\" \/><\/p>\n<a target=\"_blank\" href=\"https:\/\/www.drpeterscode.com\/index.php\"><img src=\"https:\/\/www.circuitcrush.com\/wp-content\/plugins\/dpabottomofpostpage\/apixel1x1.jpg\" ><\/a><table><\/table>","protected":false},"excerpt":{"rendered":"<p>Just as understanding binary numbers is important, so is understanding hexadecimal or hex numbers. Before we go into any detail about hexadecimal numbers, I strongly suggest you read the post on binary numbers. In it, you\u2019ll learn why binary numbers are used in digital systems. This will help you understand why hex numbers also often [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1153,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_genesis_hide_title":false,"_genesis_hide_breadcrumbs":false,"_genesis_hide_singular_image":false,"_genesis_hide_footer_widgets":false,"_genesis_custom_body_class":"","_genesis_custom_post_class":"","_genesis_layout":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[94],"tags":[101],"class_list":{"0":"post-1146","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-digital-electronics","8":"tag-hexadecimal-numbers","9":"entry"},"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"https:\/\/www.circuitcrush.com\/wp-content\/uploads\/Hexadecimal-Numbers_2.jpg","_links":{"self":[{"href":"https:\/\/www.circuitcrush.com\/wp-json\/wp\/v2\/posts\/1146","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.circuitcrush.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.circuitcrush.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.circuitcrush.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.circuitcrush.com\/wp-json\/wp\/v2\/comments?post=1146"}],"version-history":[{"count":1,"href":"https:\/\/www.circuitcrush.com\/wp-json\/wp\/v2\/posts\/1146\/revisions"}],"predecessor-version":[{"id":4319,"href":"https:\/\/www.circuitcrush.com\/wp-json\/wp\/v2\/posts\/1146\/revisions\/4319"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.circuitcrush.com\/wp-json\/wp\/v2\/media\/1153"}],"wp:attachment":[{"href":"https:\/\/www.circuitcrush.com\/wp-json\/wp\/v2\/media?parent=1146"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.circuitcrush.com\/wp-json\/wp\/v2\/categories?post=1146"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.circuitcrush.com\/wp-json\/wp\/v2\/tags?post=1146"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}