Advertisement
uwekeim

Convert file sizes back and forth between human readable

May 19th, 2013
459
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.68 KB | None | 0 0
  1. public static class SizeTranslationHelper
  2. {
  3.     /// <summary>
  4.     /// Converts bytes to human-readable, e.g. "12.5GB".
  5.     /// </summary>
  6.     public static string MakeLazy(ulong byteCount)
  7.     {
  8.         string[] suf = { @"B", @"KB", @"MB", @"GB", @"TB", @"PB", @"EB" }; //Longs run out around EB
  9.         if (byteCount == 0)
  10.             return "0" + suf[0];
  11.         var bytes = (ulong)Math.Abs((decimal)byteCount);
  12.         var place = Convert.ToInt32(Math.Floor(Math.Log(bytes, 1024ul)));
  13.         var num = Math.Round(bytes / Math.Pow(1024ul, place), 1);
  14.         return (Math.Sign((decimal)byteCount) * num).ToString(CultureInfo.InvariantCulture) + suf[place];
  15.     }
  16.  
  17.     /// <summary>
  18.     /// Converts human-readable, e.g. "12.5GB", to bytes.
  19.     /// </summary>
  20.     public static ulong TranslateLazySize(string sizeLazy)
  21.     {
  22.         sizeLazy = sizeLazy.ToLowerInvariant();
  23.  
  24.         ulong result = 0;
  25.         if (tryParse(ref result, sizeLazy, @"b", 1ul))
  26.         {
  27.             return result;
  28.         }
  29.         else if (tryParse(ref result, sizeLazy, @"kb", 1024ul))
  30.         {
  31.             return result;
  32.         }
  33.         else if (tryParse(ref result, sizeLazy, @"mb", 1024ul * 1024ul))
  34.         {
  35.             return result;
  36.         }
  37.         else if (tryParse(ref result, sizeLazy, @"gb", 1024ul * 1024ul * 1024ul))
  38.         {
  39.             return result;
  40.         }
  41.         else if (tryParse(ref result, sizeLazy, @"tb", 1024ul * 1024ul * 1024ul * 1024ul))
  42.         {
  43.             return result;
  44.         }
  45.         else if (tryParse(ref result, sizeLazy, @"pb", 1024ul * 1024ul * 1024ul * 1024ul * 1024ul))
  46.         {
  47.             return result;
  48.         }
  49.         else if (tryParse(ref result, sizeLazy, @"eb", 1024ul * 1024ul * 1024ul * 1024ul * 1024ul * 1024ul))
  50.         {
  51.             return result;
  52.         }
  53.         else
  54.         {
  55.             decimal r;
  56.             if (decimal.TryParse(sizeLazy, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out r))
  57.             {
  58.                 return (ulong)r;
  59.             }
  60.             else
  61.             {
  62.                 throw new Exception(string.Format(@"Cannot parse '{0}' to number.", sizeLazy));
  63.             }
  64.         }
  65.     }
  66.  
  67.     private static bool tryParse(ref ulong result, string sizeLazy, string suffix, ulong factor)
  68.     {
  69.         decimal r;
  70.         if (
  71.             sizeLazy.EndsWith(suffix) &&
  72.             decimal.TryParse(sizeLazy.Substring(0, sizeLazy.Length - suffix.Length), NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out r))
  73.         {
  74.             result = (ulong)(r * factor);
  75.             return true;
  76.         }
  77.         else
  78.         {
  79.             return false;
  80.         }
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement