A very common task in the IT industry is needing to convert between storage size units - bytes, kilobytes, megabytes, gigabytes, terabytes, etc. To make things even more complicated, the POSIX standard also specifies that the default output for commands like "df" and "du" must be in 512 byte block sizes.
This posting will cover a very simple and easy way to quickly convert between any of these units.
A kilobyte/megabyte/gigabyte versus a kibibyte/mebibyte/gibibyte
Historically there has been a discrepancy/dispute on how much space a kilobyte, megabyte, and gigabyte represented. If you ask a hard drive manufacturer, they would say a gigabyte is 1,000,000,000 bytes. However, most Operating Systems calculated a gigabyte as 1,073,741,824 (which is 1024*1024*1024). This is about a 7% discrepancy, and as sizes increase the discrepancy gets larger (for example a terabyte has about a 9% discrepancy.)
The solution to all this was that the official definition of a "Gigabyte" is now 1,000,000,000 bytes, and a "Gibibyte" is 1,073,741,824. See http://en.wikipedia.org/wiki/Binary_prefix for much more information.
I don't know about you, but I have never actually heard another person say the word "Gibibyte". Throughout the rest of this post I will refer to a gigabyte as 1,073,741,824 bytes as this is the common use among people even if it is incorrect per the textbook definition.
The wrong way to convert between size units
Many people will look at a filesize such as 54,183,672,092 bytes and say it is "54 Gigabytes" based on the first 2 digits of the number. In fact, it is really 50.5 Gigabytes (54,183,672,092 divided by 1,073,741,824 (size of 1 GB) equals ~ 50.5 GB). The larger the file size, the bigger the discrepancy will be between the size it appears to be at first glance and the actual size.
The quick and easy method to convert between size units
There is an extremely fast and easy way to do any of these conversions. With this method the only number you need to memorize is 1024. The only other thing you need to know is the name/order of the sizes (kilobyte, megabyte, gigabyte, terabyte). To convert smaller units to larger units (i.e. take bytes and convert to kilobytes, megabytes, gigabytes, or terabytes) you simply divide the original number by 1,024 for each unit size along the way to the final desired unit. For example, if you wanted to convert 110,214,321,212 bytes to megabytes, you would divide by 1,024 (to first covert to KB), and then divide by 1,024 again (to end up in MB). If you wanted to convert to gigabytes, you would divide by 1,024 three times (once to get to KB, once to get to MB, and then once to end up in GB).
To convert larger units to smaller units (i.e. take a number of gigabytes and convert it down in to megabytes, kilobytes, or bytes) you simply mulitply the original number by 1,024 for each unit size along the way to the final desired unit. For example, if you want to convert 384 megabytes to bytes, you would simply multiply it by 1,024 two times (first time to convert to KB, and the second time to end up in bytes). If you wanted to convert 14 terabytes to the number of bytes, you would multiply 14 by 1,024 four times (first to convert to GB, then to MB, then to KB, and then to bytes).
Here is a diagram that summarizes this:
Here are some examples:
Convert 67,003,324,746 bytes to Gigabytes: 67,003,324,746 / 1024 / 1024 / 1024 = 62.40 GB (Divide by 1024 three times because we are moving across 3 units, smaller to larger unit)
Convert 67,003,324,746 bytes to Megabytes: 67,003,324,746 / 1024 / 1024 = 63,899 MB (Divide by 1024 two times because we are moving across 2 units, smaller to larger unit)
Convert 8,846,679 Megabytes to Terabytes: 8,846,679 / 1024 / 1024 = 8.44 TB (Divide by 1024 two times because we are moving across 2 units, smaller to larger unit)
Convert 78 Gigabytes to Bytes: 78 * 1024 * 1024 * 1024 = 83,751,862,272 (Multiple by 1024 three times because we are moving across 3 units, larger to smaller unit)
Convert 52 Terabytes to Gigabytes: 52 * 1024 = 53,248 Gigabytes (Multiply by 1024 one time because we are moving across 1 unit, larger to smaller)
The final piece of the puzzle: 512 byte blocks
The POSIX standards require that the default output of commands like "df" and "du" be in 512 byte block units, so this is a unit you will run in to from time to time. A 512 byte block is exactly what it sounds like: 512 bytes of data.
There are a couple of methods to convert 512 byte blocks in to something more meaningful:
You can convert 512 byte blocks to kilobytes by dividing them by 2. For example, six 512-byte-blocks divided by two equals 3 KB.
You can convert 512 byte blocks to bytes by multiplying them by 512. For example, six 512-byte-blocks multiplied by 512 equals 3,072 bytes
Once you have converted the 512-byte-blocks in to either kilobytes or bytes, you can then easily convert them to whatever other unit you need.
Converting between size units is much easier then most people think. All you need to do is memorize the number 1,024 and a couple of other rules and you will be on your way to being able to quickly and easily convert between any size units.