Search This Blog

Title of the confirm dialog

Description of what is about to happen

File Size Conversion

Convert file size between selected unit enumeration (bit/bytes) and mode kilo bit size. Use functions and extensions for various file size data type (int, long, double)

List of functions

○ Convert.
○ To Another File Size (2 overloads).
○ Calculate Default File Size (2 overloads).
○ To Default File Size From Bits (2 overloads).
○ To Default File Size From Bytes (3 overloads).





Main class File Size Conversion

Source Code

  1 
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83 
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;

namespace TechScales
{ 
    static partial class FileSizeConverter
    {

        //'####################################################
        //' logic source:
        //' matisse bitcalc
        //'####################################################

        private const int BITS_IN_A_BYTE = 8;
        /// <summary>
        /// mode kilo bit
        /// </summary>
        internal enum eKilo : int
        {
            IEEE_1000 = 1000,
            Legacy_1024 = 1024
        }
        /// <summary>
        /// file size unit
        /// </summary>
        internal enum eUnit
        {
            /// <summary>
            /// unit b
            /// </summary>
            bits = 0,
            /// <summary>
            /// unit B
            /// </summary>
            bytes,
            /// <summary>
            /// unit Kb
            /// </summary>
            kilobits,
            /// <summary>
            /// unit KB
            /// </summary>
            kilobytes,
            /// <summary>
            /// unit Mb
            /// </summary>
            megabits,
            /// <summary>
            /// unit MB
            /// </summary>
            megabytes,
            /// <summary>
            /// unit Gb
            /// </summary>
            gigabits,
            /// <summary>
            /// unit GB
            /// </summary>
            gigabytes,
            //terabytes,
            //petabytes
        }
        internal static string UnitFileSize(eUnit e)
        {
            var s = "";
            switch (e)
            {
                case eUnit.bits: s = "bits"; break;
                case eUnit.bytes: s = "bytes"; break;
                case eUnit.kilobits: s = "Kb"; break;
                case eUnit.kilobytes: s = "KB"; break;
                case eUnit.megabits: s = "Mb"; break;
                case eUnit.megabytes: s = "MB"; break;
                case eUnit.gigabits: s = "Gb"; break;
                case eUnit.gigabytes: s = "GB"; break;
            }
            return s;
        }

    }
}

Others enum eUnit, can be added (tera, peta - bits/bytes) by removes its // sign, on line62 and line63. The content of string UnitFileSize and contents of function Convert and CalculateDefaultFileSize, must be changed too, according to enum eUnit added.

TOP