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
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
THOUSANDS_SEPERATOR = ",";