如何正确地将文件大小(以字节为单位)转换为兆或千兆字节?

问题描述 投票:0回答:8

我在 C# 项目中使用 DriveInfo 类来检索给定驱动器上的可用字节。如何正确地将这个数字转换为兆字节或千兆字节?我猜除以 1024 是不行的。结果始终与 Windows 资源管理器中显示的结果不同。

c# byte disk driveinfo megabyte
8个回答
50
投票

1024 在程序中的使用是正确的。

您可能存在差异的原因可能是由于driveinfo报告的“可用空间”和Windows认为的可用空间存在差异。

请注意,只有驱动器制造商使用 1,000。在 Windows 和大多数程序中,正确的缩放比例是 1024。

此外,虽然您的编译器无论如何都应该对此进行优化,但只需将每个量级的位移动 10 即可完成此计算:

KB = B >> 10
MB = KB >> 10 = B >> 20
GB = MB >> 10 = KB >> 20 = B >> 30

尽管为了可读性,我希望连续除以 1024 会更清晰。


27
投票

XKCD 有明确的答案

Single, definitive standard for KB


12
投票

/// <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"

11
投票

1024实际上是错误的。国际工程界 (IEC) 于 2000 年制定了一项标准,但遗憾的是该标准被计算机行业忽视。这个标准基本上说的是

  • 1000 字节是 KB,1000KB 是 1 MB,依此类推。缩写有KB、MB、GB等。
  • 广泛使用的 1024 字节 = 1 kilobyte 应该被称为 1024 字节 = 1 Kibibyte (KiB)、1024 KiB = 1 Mebibyte (MiB)、1024 MiB = 1 Gibibyte (GiB) 等等。

大家可以在IEC SI专区阅读。

因此,为了使您的转换正确且符合国际标准化,您应该使用这种科学记数法。


4
投票

这取决于您想要实际文件大小还是磁盘上的大小。实际文件大小是文件在内存中使用的实际字节数。磁盘上的大小是磁盘/文件系统的文件大小和块大小的函数。


1
投票

我依稀记得是使用1000还是1024的答案在于前缀的大小写。 例子: 如果使用“科学”1000 缩放比例,则“科学”单位将为 kB(就像公斤、kN 等)。如果使用以计算机为中心的 1024 缩放,则单位将为 KB。因此,大写科学前缀使其以计算机为中心。


0
投票

除以 1024。


0
投票

这是我准备的简单的 C++ 代码示例,可能会有所帮助。您需要提供以字节为单位的输入大小,该函数将以人类可读的大小返回:

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 ";
}
© www.soinside.com 2019 - 2024. All rights reserved.