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

发布于 2024-07-13 17:08:02 字数 116 浏览 11 评论 0原文

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

寒江雪… 2024-07-20 17:08:02

1024 对于程序中的使用是正确的。

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

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

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

KB=B>> 10
MB=KB>> 10 = B>> 20
GB=MB>> 10 = 知识库>> 20 = B>> 30

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

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:

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

Although for readability I expect successive division by 1024 is clearer.

沫离伤花 2024-07-20 17:08:02

XKCD 有明确答案

KB 的单一、权威标准

XKCD has the definite answer:

Single, definitive standard for KB

吃颗糖壮壮胆 2024-07-20 17:08:02
/// <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"
/// <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"
嘿哥们儿 2024-07-20 17:08:02

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

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

您可以在 IEC SI 专区 上阅读它。

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

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.

旧伤还要旧人安 2024-07-20 17:08:02

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

It depends on if you want the actual file size or the size on disk. The actual file size is the actual number of bytes that the file uses in memory. The size on disk is a function of the file size and the block size for your disk/file system.

放肆 2024-07-20 17:08:02

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

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.

天生の放荡 2024-07-20 17:08:02

这是我准备的简单的 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 ";
}

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 ";
}
太阳男子 2024-07-20 17:08:02

除以 1024。

Divide by 1024.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文