{"id":66613,"date":"2006-08-24T15:24:00","date_gmt":"2006-08-24T15:24:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/08\/24\/how-can-i-add-or-subtract-two-hexadecimal-numbers\/"},"modified":"2006-08-24T15:24:00","modified_gmt":"2006-08-24T15:24:00","slug":"how-can-i-add-or-subtract-two-hexadecimal-numbers","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-add-or-subtract-two-hexadecimal-numbers\/","title":{"rendered":"How Can I Add or Subtract Two Hexadecimal Numbers?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! How can I add or subtract two hexadecimal numbers?<BR><BR>&#8212; PS<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, PS. You know, according to the old saying there are two things you should never watch being made: laws and sausages. We\u2019d like to add a third item to that list: <I>Hey, Scripting Guy!<\/I> columns.<\/P>\n<P>We knew you\u2019d say that. But remember, you\u2019re seeing the finished product, a column that has been fine-tuned to perfection. You never see the process involved in creating that column, a process that doesn\u2019t exactly scream craftsmanship, if you know what we mean.<\/P>\n<P>Take this question, for example. The Scripting Guy who writes this column was pretty sure that you couldn\u2019t directly add two hexadecimal numbers; that is, there was no way to do something similar to this:<\/P><PRE class=\"codeSample\">hexNumber3 = &amp;H1C8 + &amp;HD201\n<\/PRE>\n<P>But he wasn\u2019t fazed by that; after all, he knew there was a way to convert hexadecimal numbers to decimal. Was that helpful? You bet it was: that means you can add two hexadecimal numbers together simply by converting those numbers to their decimal equivalent and then adding the decimal values. Child\u2019s play.<\/P>\n<P>Of course, to paraphrase Groucho Marx, what this Scripting Guy really needed right about then was a child. Convinced that the name of the VBScript function he was looking for was either Decimal or Dec or something like that he began a long and fruitless journey to try and convert hexadecimal numbers using code similar to this:<\/P><PRE class=\"codeSample\">intDecimal = Decimal(&#8220;&amp;H1C8&#8221;)\n<\/PRE>\n<P>Not being the type to give up, even though he was hopelessly wrong, he tried innumerable variations on Decimal, Dec, and DecToHex before finally deciding to take a look at the VBScript Language Reference. And even <I>then<\/I> he couldn\u2019t figure it out, again because, like Ponce de Leon searching for the Fountain of Youth, he was fixated on finding a function named Decimal.<\/P>\n<P>Whether such a function really existed or not.<\/P>\n<P>Several years later the light bulb finally went on, he realized which function he <I>really<\/I> needed to use, and he finally wrote the code for today\u2019s column.<\/P>\n<P>But, as far as you guys are concerned, our hero took one look at this question and <I>immediately<\/I> sat down and wrote the following script:<\/P><PRE class=\"codeSample\">hexNumber1 = &#8220;&amp;H1C8&#8221; \nhexNumber2 = &#8220;&amp;HD201&#8221;<\/p>\n<p>int1 = CLng(hexNumber1) \nint2 = CLng(hexNumber2) <\/p>\n<p>int3 = int1 + int2<\/p>\n<p>Wscript.Echo &#8220;Decimal sum: &#8221; &amp; int3\nWscript.Echo &#8220;Hexadecimal sum: &#8221; &amp; Hex(int3)\n<\/PRE>\n<P>As usual, the final product turned out to be remarkably easy. The script begins by assigning a pair of hexadecimal values to two variables; the variable hexNumber1 gets assigned the value &amp;H1C8 (456) and the variable hexNumber2 gets assigned the value &amp;HD201 (53761):<\/P><PRE class=\"codeSample\">hexNumber1 = &#8220;&amp;H1C8&#8221; \nhexNumber2 = &#8220;&amp;HD201&#8221;\n<\/PRE>\n<P>There\u2019s nothing special here; we just needed a pair of hexadecimal numbers we could play with.<\/P>\n<P>Once we have those numbers we need to convert the values to their decimal equivalents. That\u2019s the very task our \u2026 hero \u2026 was trying to perform by calling the non-existent function Decimal. As it turns out, it\u2019s way better to use <I>real<\/I> VBScript functions \u2013 like <B>CLng<\/B> \u2013 than it is to use make-believe VBScript functions. In other words:<\/P><PRE class=\"codeSample\">int1 = CLng(hexNumber1) \nint2 = CLng(hexNumber2)\n<\/PRE>\n<TABLE id=\"ECE\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. Actually, the terms <I>CLng<\/I> and <I>Decimal<\/I> are so similar you can see why the Scripting Guy who writes this column got confused. No doubt many, many people make the same mistake each and every day.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>As soon as we\u2019ve converted the hex values to their decimal equivalents we can then add them (or subtract them or multiply them or divide them or \u2026). How? By using code similar to this:<\/P><PRE class=\"codeSample\">int3 = int1 + int2\n<\/PRE>\n<P>After that all we have to do is echo back the value of the variable int3 (which contains the sum of the two hexadecimal numbers). In our sample script we echo back the decimal value of the sum and then, just for the heck of it, we use the <B>Hex<\/B> function to convert the sum back into a hexadecimal value and echo that value:<\/P><PRE class=\"codeSample\">Wscript.Echo &#8220;Decimal sum: &#8221; &amp; int3\nWscript.Echo &#8220;Hexadecimal sum: &#8221; &amp; Hex(int3)\n<\/PRE>\n<P>When we run the script, we should get back something similar to this:<\/P><PRE class=\"codeSample\">Decimal sum: 54217\nHexadecimal sum: D3C9\n<\/PRE>\n<TABLE id=\"E4E\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. We don\u2019t blame you; we don\u2019t trust him, either. To double check his math, open up Calculator, set the <B>View<\/B> to <B>Scientific<\/B>, and enter the equation yourself.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>There you have it, PS; considering the amount of time it took to finally finish this column we hope you find the end result useful. To be honest, we wish the process would have gone a little smoother ourselves. But, then again, this is a column on system administration scripting; just think how boring <I>that<\/I> would be if one of the other Scripting Guys (that is, someone who knew what they were doing) wrote it. You guys don\u2019t know how lucky you are!<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I add or subtract two hexadecimal numbers?&#8212; PS Hey, PS. You know, according to the old saying there are two things you should never watch being made: laws and sausages. We\u2019d like to add a third item to that list: Hey, Scripting Guy! columns. We knew you\u2019d say that. But [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[51,3,4,5],"class_list":["post-66613","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-scripting-guy","tag-scripting-techniques","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I add or subtract two hexadecimal numbers?&#8212; PS Hey, PS. You know, according to the old saying there are two things you should never watch being made: laws and sausages. We\u2019d like to add a third item to that list: Hey, Scripting Guy! columns. We knew you\u2019d say that. But [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66613","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=66613"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66613\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=66613"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=66613"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=66613"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}