23

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.

Sнаđошƒаӽ's user avatar

Sнаđошƒаӽ

17.9k13 gold badges79 silver badges96 bronze badges

asked Feb 19, 2009 at 15:03

Mats's user avatar

1

54

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.

GEOCHET's user avatar

GEOCHET

21.2k15 gold badges78 silver badges99 bronze badges

answered Feb 19, 2009 at 15:05

Adam Davis's user avatar

6 Comments

Yes, only drive manufacturers use the 1,000-based definitions. That's why your "4.7 GB" drives are actually 4,700,372,992 bytes, "50 GB" Blu-Ray is only 50,050,629,632 bytes, your "128 kbps" mp3 is actually 128,000 bits per second, a "3.1 megapixel" camera only has 3,145,728 pixels, a 2 GHz processor is actually 2,000,000,000 hertz, a 16 GB/s memory bus is actually 16,000,000 bytes per second, ...

1024 is only "correct" in that it represents the actual number of bits on the disk. If you're concerned about correctness however, you need to display that with the unit "GiB". Using 1024 and calling it a "GB" is technically incorrect. GB is officially 1000 million, not 1024 million. I'd argue that 1000 is the correct usage from a human factors point of view. 1 gigawatts is 1000 megawatts. 1 GHz is 1000 MHz. By the same token (and for easier math by humans) 1000 gigabytes is 1000 megabytes.

No @Adam, @Bryan is correct. It doesn't make sense that the standard SI prefixes would mean different things when your talking about "drives" or "bytes", it's powers of 10, not powers of 2. e.g. When using powers of 2, the correct term would be mebibytes, not megabytes.

@Bryan, @john - Users will complain if your program reports a different number than the OS and EVERY other program. If you want a reasonable user experience you should seriously consider sticking to the common convention. However, it's your program to do with as you please, and if you're ready to deal with customer questions regarding what a GiB is, or why your software reports different drive sizes than everything else they see, go for it. Keep in mind that the SI prefixes are great for technical documentation, but not necessarily designed for the end user to understand easily.

Apple software reports file size and drive storage as 1 GB = 1 billion bytes, and I think I've seen a few other programs do the same. The whole kilo- = 1024 thing arose at a time when saving a few bits on addressable space was a big deal and saving a couple of bit per variable dividing by 1024 was much computationally cheaper than dividing by 1000, but with modern hardware it hardly makes a difference. There's really not much to be gained from caring much about the difference, though. Do whatever makes you happy since some people are going to tell you you're wrong no matter what.

|

28

Community's user avatar

answered Feb 19, 2009 at 15:16

starblue's user avatar

starblue

57.1k14 gold badges102 silver badges153 bronze badges

3 Comments

@Adam Davis For ECC RAM that is actually exactly what is done in practice.

while funny, it does not answer the question and may confuse those who are easily confused.

Some machines have non-standard architectures with more or fewer bits per byte than 8. I'm thinking of the UNIVAC 1103/1103A/1105/1100/2200 mainframes with a 9-bits per byte (and the potential to run programs that only use 6-bits per byte). So the KBa would no longer by the Baker's Kilobyte on those machines, but the standard.

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"

Brad Gilbert's user avatar

Brad Gilbert

34.3k11 gold badges80 silver badges131 bronze badges

answered Oct 21, 2010 at 6:11

AZ_'s user avatar

AZ_

22k26 gold badges149 silver badges194 bronze badges

Comments

10

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.

answered Feb 19, 2009 at 16:24

grover

5 Comments

Standard or no, it would be ridiculous to report disk usage in a different way than the host OS!

The problem is with those host OS doing it wrong and any end user wondering why the brand new disk he bought in the store reports less once installed than what's printed on its box.

re: "it would be ridiculous to report disk usage in a different way than the host OS": Snow Leopard (Mac OSX 10.6) uses the value 1000 (support.apple.com/kb/TS2419)

-1 for the statement of "1024 is actually wrong." IEC might say 1000, but GNU says 1024 (try doing a df -h in Linux). It looks like IEC is just a pawn of hard drive manufacturers.

Use the standard ignore the others! kB decimal KB binary

4

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.

answered Feb 19, 2009 at 15:07

EBGreen's user avatar

EBGreen

38.1k12 gold badges69 silver badges86 bronze badges

Comments

2

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 ";
}

answered Sep 30, 2022 at 18:15

MD. Nazmul Kibria's user avatar

1 Comment

There is an error in if( bytes >= kb) return std::to_string( (float)bytes/gb ) + " KB ";, it should be kb instead of gb

1

Divide by 1024.

answered Feb 19, 2009 at 15:06

Fabian Vilers's user avatar

Comments

1

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.

answered Jan 8, 2010 at 12:34

Anders's user avatar

2 Comments

KB = 1000 bytes, KiB = 1024 bytes

+1 For k vs. K this is correct, but it doesn't help when the letter for the ISO prefix is already uppercase, as in M, G and T for Mega, Giga and Tera. And many people, especially from the US, have a tendency to mix up the case of ISO units and prefixes.

0

From SSD or memory hardware hw POV. A memory Byte (8 bits) is addressed using binary address (hw) conductors. So 10 address conductors is 2^^10=1024 call it KB. 20 address conductors is 2^^20=1048576 call it MB. 30 address conductors is 2^^30=1073741824 call it TB. The notation doesn’t change the exact number. There is no such thing as a 1,000,000,000 TB in computer memory.

answered Nov 20, 2025 at 3:03

Rik Humphries's user avatar

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.