I'm using the DriveInfo class in my C# project to retrieve the available bytes on given drives. How to I correctly convert this number into Mega- or Gigabytes? Dividing by 1024 will not do the job I guess. The results always differ from those shown in the Windows-Explorer.
How to correctly convert filesize in bytes into mega or gigabytes?
68.4k views Asked by Mats AtThere are 8 answers
1024 is actually wrong. The International Engineering Community (IEC) has developed a standard in 2000, which is sadly being ignored by the computer industry. This standard basically says that
- 1000 bytes is a kilobyte, 1000KB are one MB and so on. The abbreviations are KB, MB, GB and so on.
- The widely used 1024 bytes = 1 kilobyte should instead by called 1024 bytes = 1 Kibibyte (KiB), 1024 KiB = 1 Mebibyte (MiB), 1024 MiB = 1 Gibibyte (GiB) and so on.
You can all read it up on the IEC SI zone.
So in order for your conversions to be correct and right according to international standardization you should use this scientific notation.
/// <summary>
/// Function to convert the given bytes to either Kilobyte, Megabyte, or Gigabyte
/// </summary>
/// <param name="bytes">Double -> Total bytes to be converted</param>
/// <param name="type">String -> Type of conversion to perform</param>
/// <returns>Int32 -> Converted bytes</returns>
/// <remarks></remarks>
public static double ConvertSize(double bytes, string type)
{
try
{
const int CONVERSION_VALUE = 1024;
//determine what conversion they want
switch (type)
{
case "BY":
//convert to bytes (default)
return bytes;
break;
case "KB":
//convert to kilobytes
return (bytes / CONVERSION_VALUE);
break;
case "MB":
//convert to megabytes
return (bytes / CalculateSquare(CONVERSION_VALUE));
break;
case "GB":
//convert to gigabytes
return (bytes / CalculateCube(CONVERSION_VALUE));
break;
default:
//default
return bytes;
break;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return 0;
}
}
/// <summary>
/// Function to calculate the square of the provided number
/// </summary>
/// <param name="number">Int32 -> Number to be squared</param>
/// <returns>Double -> THe provided number squared</returns>
/// <remarks></remarks>
public static double CalculateSquare(Int32 number)
{
return Math.Pow(number, 2);
}
/// <summary>
/// Function to calculate the cube of the provided number
/// </summary>
/// <param name="number">Int32 -> Number to be cubed</param>
/// <returns>Double -> THe provided number cubed</returns>
/// <remarks></remarks>
public static double CalculateCube(Int32 number)
{
return Math.Pow(number, 3);
}
//Sample Useage
String Size = "File is " + ConvertSize(250222,"MB") + " Megabytes in size"
Here is simple c++ code sample I have prepared which might be helpful. You need to provide input size in bytes and the function will return in human readable size:
std::string get_human_readable_size(long bytes)
{
long gb = 1024 * 1024 * 1024;
long mb = 1024 * 1024;
long kb = 1024;
if( bytes >= gb) return std::to_string( (float)bytes/gb ) + " GB ";
if( bytes >= mb) return std::to_string( (float)bytes/mb ) + " MB ";
if( bytes >= kb) return std::to_string( (float)bytes/kb ) + " KB ";
return std::to_string(bytes) + " B ";
}
I have a faint recollection that the answer on whether to use 1000 or 1024 lies in the casing of the prefix. Example: If the "scientific" 1000 scaling is used, then the "scientific" unit will be kB (just as in kg, kN etc). If the computer centric 1024 scaling is used, then the unit will be KB. So, uppercasing the scientific prefix makes it computer centric.
1024 is correct for usage in programs.
The reason you may be having differences is likely due to differences in what driveinfo reports as "available space" and what windows considers available space.
Note that only drive manufacturers use 1,000. Within windows and most programs the correct scaling is 1024.
Also, while your compiler should optimize this anyway, this calculation can be done by merely shifting the bits by 10 for each magnitude:
Although for readability I expect successive division by 1024 is clearer.