views:

48

answers:

2

Actually, I'm not asking how to implement this functionality myself. I know it wouldn't be very complicated. I just don't want to reinvent the wheel, so I was wondering if this functionality exists somewhere in the BCL. It seems like surely it's there somewhere...

Example input/desired output:

Input       Output
1           1
2           10
3           11
4           100
10          1010
+7  A: 

Try Convert.ToString, like this:

Console.WriteLine(Convert.ToString(1, 2));
Console.WriteLine(Convert.ToString(2, 2));
Console.WriteLine(Convert.ToString(3, 2));
Console.WriteLine(Convert.ToString(4, 2));
Console.WriteLine(Convert.ToString(10, 2));

The second parameter is the base to use to convert the number (in this case, base 2).

Mark
+10  A: 

How about System.Convert.ToString(int value, int toBase) with toBase set to 2?

GBegen
Nice, how did I miss this one? (I always forget about the `Convert` class!)
Dan Tao
I'll have to keep this in mind, this is really handy and elegant!
Mark LeMoine