Byte Conversions Made Easy

To save time and eliminate calculator errors, use this HTML Application (HTA) to perform byte conversions. It converts values using both factors of 2 (kilo = 1,024 bytes) and 10 (kilo = 1,000 bytes).

Bill Stewart

April 26, 2011

3 Min Read
ITPro Today logo


Microsoft and other application vendors represent disk space and memory sizes in bytes, kilobytes, megabytes, gigabytes, and so forth. For example, 1,024 kilobytes equals 1MB. It’s a common task to convert these values to different units. For example, you might have to convert 16GB into kilobytes.

Storage vendors add an interesting wrinkle to the conversions because they calculate these units differently. They typically use factors of 10 instead of 2. For example, a “kilo” represents 1,000 bytes instead of 1,024 bytes. This difference can cause problems when you need to know, for example, how much tape is needed to back up a database. A database that uses 400GB on a disk won’t fit on a 400GB tape because 400GB means 409,600 megabytes to the OS but only 400,000 megabytes to a storage vendor.

I often use a calculator to convert between bytes, kilobytes, and so forth. To eliminate calculator errors and save myself some time, I decided to write an HTML Application (HTA), ByteCalc.hta, to perform these conversions. ByteCalc.hta converts values using both factors of 2 (kilo = 1,024 bytes) and 10 (kilo = 1,000 bytes).

To run ByteCalc.hta, simply double-click it from Windows Explorer or enter its filename at a command prompt. (You can download the HTA by going to the top of this page and click the Download the Code Here button.) In the HTA’s UI, which Figure 1 shows, enter a number in the Value field, choose the unit in which the number is expressed (e.g., gigabytes, megabytes), and click the Calculate button.


Figure 1: HTA’s UI

ByteCalc.hta uses the and HTML elements combined with JScript (Microsoft’s version of JavaScript) code to perform the conversions. To calculate the proper values, it uses the element’s currently selected element to determine which values get placed into the corresponding fields on the form. Table 1 shows the formulas that ByteCalc.hta uses to convert bytes. Table 2 shows the formulas it uses to convert megabytes. As you can see in Tables 1 and 2, ByteCalc.hta either divides or multiplies by a factor (depending on whether kilo = 1,024 or 1,000) to achieve its results.


Table 1: Converting from Bytes
Table 2: Converting from Megabytes

By default, ByteCalc.hta uses a comma (,) as the thousands separator and includes thousands separators in its results. If you don't want to use thousands separators, clear the Use thousands separator check box before clicking the Calculate button. If the Use thousands separator check box is selected, you can enter a different character for the thousands separator.

You can easily configure ByteCalc.hta not to use thousands separators by default. Open ByteCalc.hta in Notepad (or another plain-text editor) and locate the two lines of code shown in Listing 1. Change the THOUSANDS_SEPARATOR_DEFAULT variable from true to false (the words true and false must be lowercase). If you want to use a thousands separator but you want it to be some other character by default, replace the comma between the double quotes to the character you want to use.


Listing 1: Code to Customize in ByteCalc.hta

var THOUSANDS_SEPARATOR_DEFAULT = true,THOUSANDS_SEPERATOR = ",";


Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like