3

I'm looking for a string conversion method that will receive an input of KB/s and converts it to the easiest readable format possible.

e.g. 1500 b/s = 1.46 Kb/s
e.g. 1500 Kb/s = 1.46 Mb/s
e.g. 1500 Mb/s = 1.46 Gb/s

Thanks

Peter
  • 14,221
  • 15
  • 70
  • 110

2 Answers2

7

Try this:

var ordinals = new [] {"","K","M","G","T","P","E"};

long bandwidth = GetTheBandwidthInBitsPerSec();

decimal rate = (decimal)bandwidth;

var ordinal = 0;

while(rate > 1024)
{
   rate /= 1024;
   ordinal++;
}

output.Write(String.Format("Bandwidth: {0} {1}b/s", 
   Math.Round(rate, 2, MidpointRounding.AwayFromZero), 
   ordinals[ordinal]));

The ordinals (prefixes) available here are Kilo-, Mega-, Giga-, Tera-, Peta-, Exa-. If you really think your program will be around long enough to see Zettabit and Yottabit network bandwidths, by all means throw the Z and Y prefix initials into the array.

To convert from one formatted string to the other, split on spaces, look at the term that will be the number, and then search the term immediately following for one of the prefixes. Find the index of the ordinal in the array, add 1, and multiply by 1024 that many times to get to bits per second:

var bwString= GetBandwidthAsFormattedString(); //returns "Bandwidth: 1056 Kb/s";

var parts = String.Split(bwString, " ");
var number = decimal.Parse(parts[1]);
var ordinalChar = parts[2].First().ToString();
ordinalChar = ordinalChar = "b" ? "" : ordinalChar;

var ordinal = ordinals.IndexOf(ordinalChar)

... //previous code, substituting the definition of ordinal
KeithS
  • 70,210
  • 21
  • 112
  • 164
  • I really like this one :D Thanks! – Peter Sep 17 '10 at 19:42
  • Very nice - but the prefix for 10^3 is 'k', not 'K'. – Duncan Bayne Mar 08 '11 at 04:01
  • Picky, picky. I'm looking at Windows Explorer, and every file size is listed in "KB". I know that technically you're right, but it's the only such case for a prefix used in computing, so in computing it's virtually always K and not k. The only exception I can think of is in dialup baud rates; 56 kbps, not 56 Kbps. – KeithS Mar 08 '11 at 16:26
  • Having looked into it further, I think we're both wrong according to the IEC :-). Your example is actually 2^10, so it should be "Ki" whereas "k" is strictly speaking reserved for 10^3. See: http://en.wikipedia.org/wiki/Binary_prefix . For what it's worth, I'm currently working on an app to be used in part by mathematicians, so I'm trying to get this stuff Right (TM). – Duncan Bayne Mar 08 '11 at 22:04
  • Fair play to ya. In that case, whether you change 1024 to 1000 and K to k, or change all the prefixes to add "i", depends on whether you're converting data size or a "real" metric measurement. – KeithS Mar 08 '11 at 22:22
1

I made this code in about 30 secondes, so there is no validation, but I think it does what you want

        string vInput = "1500 Kb/s";
        string[] tSize = new string[] { "b/s", "Kb/s", "Mb/s", "Gb/s" };
        string[] tSplit = vInput.Split(new string[] {" "}, StringSplitOptions.RemoveEmptyEntries);
        double vSpeed = Double.Parse(tSplit[0]) / 1024.0;
        vSpeed = Math.Round(vSpeed, 2);
        int i = 0;
        for(i = 0; i < tSize.Length;++i)
        {
            if(tSplit[1].StartsWith(tSize[i]))
            {
                break;
            }
        }
        string vOutput = vSpeed.ToString() + " " + tSize[i+1];
Wildhorn
  • 926
  • 1
  • 11
  • 30