How Can I Add or Subtract Two Hexadecimal Numbers?


Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I add or subtract two hexadecimal numbers?

— PS

SpacerHey, Scripting Guy! AnswerScript Center

Hey, PS. You know, according to the old saying there are two things you should never watch being made: laws and sausages. We’d like to add a third item to that list: Hey, Scripting Guy! columns.

We knew you’d say that. But remember, you’re 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’t exactly scream craftsmanship, if you know what we mean.

Take this question, for example. The Scripting Guy who writes this column was pretty sure that you couldn’t directly add two hexadecimal numbers; that is, there was no way to do something similar to this:

hexNumber3 = &H1C8 + &HD201

But he wasn’t 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’s play.

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:

intDecimal = Decimal(“&H1C8”)

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 then he couldn’t figure it out, again because, like Ponce de Leon searching for the Fountain of Youth, he was fixated on finding a function named Decimal.

Whether such a function really existed or not.

Several years later the light bulb finally went on, he realized which function he really needed to use, and he finally wrote the code for today’s column.

But, as far as you guys are concerned, our hero took one look at this question and immediately sat down and wrote the following script:

hexNumber1 = “&H1C8” 
hexNumber2 = “&HD201”

int1 = CLng(hexNumber1) int2 = CLng(hexNumber2)

int3 = int1 + int2

Wscript.Echo “Decimal sum: ” & int3 Wscript.Echo “Hexadecimal sum: ” & Hex(int3)

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 &H1C8 (456) and the variable hexNumber2 gets assigned the value &HD201 (53761):

hexNumber1 = “&H1C8” 
hexNumber2 = “&HD201”

There’s nothing special here; we just needed a pair of hexadecimal numbers we could play with.

Once we have those numbers we need to convert the values to their decimal equivalents. That’s the very task our … hero … was trying to perform by calling the non-existent function Decimal. As it turns out, it’s way better to use real VBScript functions – like CLng – than it is to use make-believe VBScript functions. In other words:

int1 = CLng(hexNumber1) 
int2 = CLng(hexNumber2)

Note. Actually, the terms CLng and Decimal 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.

As soon as we’ve converted the hex values to their decimal equivalents we can then add them (or subtract them or multiply them or divide them or …). How? By using code similar to this:

int3 = int1 + int2

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 Hex function to convert the sum back into a hexadecimal value and echo that value:

Wscript.Echo “Decimal sum: ” & int3
Wscript.Echo “Hexadecimal sum: ” & Hex(int3)

When we run the script, we should get back something similar to this:

Decimal sum: 54217
Hexadecimal sum: D3C9

Note. We don’t blame you; we don’t trust him, either. To double check his math, open up Calculator, set the View to Scientific, and enter the equation yourself.

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 that would be if one of the other Scripting Guys (that is, someone who knew what they were doing) wrote it. You guys don’t know how lucky you are!


Discussion is closed.

Feedback usabilla icon