Create a Cool Command-Line CalculatorCreate a Cool Command-Line Calculator

Crunch numbers and see the results in three formats

Bill Stewart

August 7, 2005

I often find it useful to get quick answers to mathematical expressions. In the past, I relied on the Windows Calculator or a spreadsheet tool to get an answer, but this often interrupted my work at the command prompt. I thought it would be useful to have a command-line calculator, so I wrote a script, Eval.js, that creates a command-line calculator and outputs results in decimal, hexadecimal, and binary formats. Listing 1 shows an excerpt from this script. You can download the entire script from the Windows Scripting Solutions Web site. Go to http://www.windowsitpro.com/windowsscripting, enter 47187 in the InstantDoc ID text box, then click the 47187.zip hotlink.

Inside Eval.js
Eval.js first declares the set of variables it will use. Next, it examines the length property of the WScript.Arguments collection, which returns the number of items in the collection. If there are no command-line arguments, or if the first argument is /?, the script displays a short usage message and exits.

Next, the script retrieves the expression from the command line. To do so, it creates an Enumerator object to iterate the WScript.Arguments collection. (In JScript, you have to use an Enumerator object to iterate the members of a collection.)

Eval.js then uses the for statement to retrieve each item in the collection and builds a string in the expr variable. The string contains all the command-line arguments, separated by spaces. The script uses the conditional ternary (?:) operator to build the string. As callout A in Listing 1 shows, if the string stored in the expr variable isn't empty, the script appends a space followed by the next argument; otherwise, it appends the first argument. The script then creates an output string containing the expression followed by two new lines.

All that's left for the script to do is to attempt to evaluate the expression using the eval() function. Because the expr variable might not contain a valid JScript expression, the eval() function is enclosed in a try...catch...finally statement, as callout B shows. If an error occurs, the script displays the expression and an error message, then exits with a non-zero exit code. If no error occurs, the script uses the toString method to output the expression followed by the results of the expression in decimal, hex, and binary formats.

Possible Uses
To run Eval.js, you use the command-line syntax

`Eval.js Expression`

where Expression is the expression you want to evaluate. You can use any valid JScript computational or bitwise operator. For example, you can use the addition operator (+) to add numbers or the division operator (/) to divide numbers. (For a list of the JScript operators, go to http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/js56jsconoperators.asp.)

However, an expression doesn't have to contain an operator. You can simply specify a decimal value to quickly see the hex and binary equivalents. You can even specify a hex value to see the decimal and binary equivalents. You must, though, prefix the hex value with 0x or 0X (e.g., 0x41). Note that if you prefix a hex value with only a 0, JScript will evaluate it as an octal (base-8) number. Also note that you can't specify a binary value to see the decimal and hex equivalents because JScript doesn't have a way to specify a binary number.

When you use Eval.js, you need to watch out for the following reserved shell characters:

`( ) < > ^ & |`

If an expression contains any of these characters, you must enclose it in quotes (or more awkwardly, escape each one with the ^ character). For example, the expression (2 + 3) * 6 should be enclosed in quotes because it contains the ( and ) characters. The quotes won't be considered as part of the expression and will prevent cmd.exe from interpreting the special characters.