C# || How To Convert Bytes To Kilobytes, Megabytes, Gigabytes, Terabytes Using C#

Print Friendly, PDF & Email

The following is a module with functions which demonstrates how to convert bytes to decimal formats like kilobytes, megabytes, gigabytes, terabytes, petabytes, exabytes, zettabytes, and yottabytes, as well as binary formats like kibibytes, mebibytes, gibibytes, tebibytes, pebibytes, exbibytes, zebibytes, and yobibytes using C#.

The function demonstrated on this page follows the IEC standard, which means:


• 1 kilobyte = 1000 bytes (Decimal)
• 1 kibibyte = 1024 bytes (Binary)

This function allows you to convert bytes to a measurement unit, a measurement unit to bytes, and allows to convert from one measurement unit to another measurement unit.


1. Convert Bytes To Measurement Unit

The example below demonstrates the use of ‘Utils.Bytes.FromTo‘ to convert bytes to a measurement unit.

The optional function parameter allows you to specify the decimal places.


2. Convert Measurement Unit To Bytes

The example below demonstrates the use of ‘Utils.Bytes.FromTo‘ to convert a measurement unit to bytes.

The optional function parameter allows you to specify the decimal places.


3. Convert Measurement Unit To Measurement Unit

The example below demonstrates the use of ‘Utils.Bytes.FromTo‘ to convert a measurement unit to another measurement unit.

The optional function parameter allows you to specify the decimal places.


4. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


5. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Was this article helpful?
YesNo

Leave a Reply