shalini
shalini

Reputation: 45

How to convert TB to bytes in C#

I need to convert TB's or anything more than 80 GB to bytes. How would I do this in C#? Which data type I have to use?

int TBsize = 1;
longbytes TBtoBytes = 1024*1024*1024*1024*1;

Upvotes: 1

Views: 855

Answers (2)

dotomu
dotomu

Reputation: 13

Source

This is a good answer / code snippet for an extension method that enables you to easily convert a long / Int64 value from bytes to a selected unit e.g. 10,000 Byte to 10 MB and return it as a rounded string.

This snippet from user NeverHopeless cleverly uses the enum type's const value association (e.g. Byte = 0, kb = 1, MB = 2) for the size conversion. These const values are based on their order inside the enum and could be overwritten if needed. The ToSize() method then uses the enums' const value with Math.Pow() for the calculation.

My Adjustments


  1. Change of original calculation for decimal units enum as well as adding a second enum for binary units, to follow the IEC standards like omar's ByteSize NuGet, as the binary and decimal units have difference in their multiplication factor:

    • decimal (e.g. MiB, GiB, TiB): factor of 1,000
    • binary (e.g. MB, GB, TB): factor of 1,024
  2. Adding second parameter to not only define the conversion target but also the source's unit to enable back-and-forth-conversion.

  3. Change of return value double instead of string, so you can compare your values more easily.

  4. Change of parameter value to double / Double instead of long / Int64 for easier back-and-forth-conversion.

Snippet


This is the code snippet for the extension class with my mentioned adjustments:

public static class SizeExtension
{
    public enum DecimalUnits
    {
        Byte, kB, MB, GB, TB, PB, EB, ZB, YB, RB, QB
    }
    
    public enum BinaryUnits
    {
        Byte, KiB, MiB, GiB, TiB, PiB, EiB, ZiB, YiB
    }

    public static double ToSize(this Double value, DecimalUnits sourceUnit, DecimalUnits targetUnit)
    {
        return (value / Math.Pow(1000, (Int64)(targetUnit - sourceUnit)));
    }

    public static double ToSize(this Double value, BinaryUnits sourceUnit, BinaryUnits targetUnit)
    {
        return (value / Math.Pow(1024, (Int64)(targetUnit - sourceUnit)));
    }
}

Examples


Now you can convert your size values like this:

// Bytes to MegaBytes:
var sizeInBytes = (double)20000;
var sizeInMB = sizeInBytes.ToSize(
    sourceUnit: SizeExtension.DecimalUnits.MB,
    targetUnit: SizeExtension.DecimalUnits.Bytes
);
// sizeInMB == (double)20;

And you can easily convert them back:

// MegaBytes to Bytes:
var sizeInBytes = sizeInMB.ToSize(SizeExtension.DecimalUnits.MB, SizeExtension.DecimalUnits.Bytes);

Results from my perspective in a more flexible approach to handling size conversions with little overhead or dependencies on external code.

Also, for clarity, I shared this code already here as an answer.

Upvotes: 0

Yacoub Massad
Yacoub Massad

Reputation: 27861

You need to use the long type and the L letter like this:

long TBtoBytes = 1024L*1024L*1024L*1024L;

The L here is used to tell the compiler that this is a long, not an int.

You can specify the terabyte value like this also:

long TBtoBytes = 1099511627776;

You don't need to use the L here.

Now you can calculate 80TB like this:

long eighty_terabytes = TBtoBytes * 80;

Upvotes: 6

Related Questions