[Edit]
+
0
-
0

C# / .NET - convert size in bytes to KB, MB, GB, TB, PB

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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
using System; using System.Collections.Generic; public class Program { public static void Main() { // --------------------------------------------------- // Practical example 1: // Used pre-default SHORT_UNITS units. // --------------------------------------------------- Dictionary<string, long?> parts = SizeUtils.calculateParts(31108834345654325); Console.WriteLine(parts["PB"] + "PB " + parts["TB"] + "TB " + parts["GB"] + "GB " + parts["MB"] + "MB " + parts["KB"] + "KB " + parts["B"] + "B"); // Output: // // 27PB 645TB 327GB 712MB 806KB 52B // --------------------------------------------------- // Practical example 2: // Used only 2 units: B and KB. // --------------------------------------------------- Console.WriteLine(SizeUtils.renderSize(1234567890, new string[] { "B", "KB"})); // 1177KB 722B // Output: // // 1177KB 722B // --------------------------------------------------- // Practical example 3: // Used pre-default SHORT_UNITS units. // --------------------------------------------------- Console.WriteLine(SizeUtils.renderSize(0)); // 0B Console.WriteLine(SizeUtils.renderSize(10)); // 10B Console.WriteLine(SizeUtils.renderSize(10240)); // 10KB Console.WriteLine(SizeUtils.renderSize(10485760)); // 10MB Console.WriteLine(SizeUtils.renderSize(10737418240)); // 10GB Console.WriteLine(SizeUtils.renderSize(10995116277760)); // 10TB Console.WriteLine(SizeUtils.renderSize(31108834345654325)); // 27PB 645TB 327GB 712MB 806KB 52B // ... 645-TB ... // | // v Console.WriteLine(SizeUtils.renderSize(31108834345654325, SizeUtils.SHORT_UNITS, "-", " ")); // 27-PB 645-TB 327-GB 712-MB 806-KB 52-B // ^ // | // ... 645-TB 327-GB ... // Output: // // 0B // 10B // 10KB // 10MB // 10GB // 10TB // 27PB 645TB 327GB 712MB 806KB 52B // 27-PB 645-TB 327-GB 712-MB 806-KB 52-B // --------------------------------------------------- // Practical example 4: // Used pre-default FULL_UNITS units with separators. // --------------------------------------------------- SizeUtils.printSize(0, SizeUtils.FULL_UNITS, " ", ", "); // 0 bytes SizeUtils.printSize(10, SizeUtils.FULL_UNITS, " ", ", "); // 10 bytes SizeUtils.printSize(10240, SizeUtils.FULL_UNITS, " ", ", "); // 10 kilobytes SizeUtils.printSize(10485760, SizeUtils.FULL_UNITS, " ", ", "); // 10 megabytes SizeUtils.printSize(10737418240, SizeUtils.FULL_UNITS, " ", ", "); // 10 gigabytes SizeUtils.printSize(10995116277760, SizeUtils.FULL_UNITS, " ", ", "); // 10 terabytes // ... 645 terabytes ... // | // v SizeUtils.printSize(31108834345654325, SizeUtils.FULL_UNITS, " ", ", "); // 27 petabytes, 645 terabytes, 327 gigabytes, 712 megabytes, 806 kilobytes, 52 bytes // ^^ // || // ... 645 terabytes, 327 gigabytes ... // Output: // // 0 bytes // 10 bytes // 10 kilobytes // 10 megabytes // 10 gigabytes // 10 terabytes // 27 petabytes, 645 terabytes, 327 gigabytes, 712 megabytes, 806 kilobytes, 52 bytes } } // --------------------------------------------------- // SizeUtils.cs file: // --------------------------------------------------- public class SizeUtils { public static readonly string[] SHORT_UNITS = { "B", "KB", "MB", "GB", "TB", "PB" }; public static readonly string[] FULL_UNITS = { "bytes", "kilobytes", "megabytes", "gigabytes", "terabytes", "petabytes" }; public static Dictionary<string, long?> calculateParts(long size, string[] units) { string ending = "$$_UNDEFINED_$$"; Dictionary<string, long?> result = new Dictionary<string, long?>(); foreach (string unit in units) { long total = (long)Math.Floor(size / 1024.0); long rest = size - 1024 * total; if (rest > 0) result.Add(unit, rest); ending = unit; size = total; } if (size > 0) { long? value = null; result[ending] = (result.TryGetValue(ending, out value) ? size + value : size); } return result; } public static Dictionary<string, long?> calculateParts(long size) { return calculateParts(size, SHORT_UNITS); } public static string renderSize(long size, string[] units, string spacer = "", string delimiter = " ") { Dictionary<string, long?> parts = calculateParts(size, units); string result = ""; foreach (KeyValuePair<string, long?> pair in parts) { if (result.Length > 0) result = delimiter + result; result = pair.Value + spacer + pair.Key + result; } if (result.Length == 0) return "0" + spacer + (units.Length > 0 ? units[0] : "B"); return result; } public static string renderSize(long size) { return renderSize(size, SHORT_UNITS); } public static void printSize(long size, string[] units, string spacer = "", string delimiter = " ") { Console.WriteLine(renderSize(size, units, spacer, delimiter)); } public static void printSize(long size) { Console.WriteLine(renderSize(size, SHORT_UNITS)); } }