This is finals week – thank God. Two classes that were particularly tough this semester were Statistics and also Computer Architecture. In the Computer Architecture class, one of the things we had to do and that was on EVERY stupid exam, was converting numbers from different bases, or radices. So, we’d have a question like:

127_{10}+ 3112_{3}– 1001011_{2}= ?

meaning, what is 127 (in base-10), plus 3112 (in base-3), minus 1001011 (in base-2) – and of course, the answers would be in some other weird base. Ugh. So, being the creative nerd I am, I wrote a program to handle this. Let me be clear first though – this can handle INPUTs from Base-2 to Base-10, and can OUTPUT from Base-2 to Base-10. Once you get above Base-10, you have to start dealing with letters and I didn’t mess with that. For the most part, any base above 10 was just Base-16, and I could just easily convert that to decimal. So, if you need Base-2 to Base-10 – this post should get you all set.

“So why are you writing a blog post about this?” you might ask. Well, because I spent probably 2 hours looking on the web for some sample code that would do this, and I found virtually nothing useful. So, if someone searches for “C# convert radix” – hopefully this page will show up and those poor souls can get an easy answer!

**The Input:**

Ok, so the first part of this, is we want to be able to accept input that is anything from Base-2 (binary) to Base-10 (decimal). Ultimately, we want to output that number as something else, but we MUST convert that input to a common base, before we can process it. To keep things simple, let’s convert everything to Base-10. So, when you give a number and specify the radix (using whatever UI you like), you can do something like this:

private static int ToBase10(int valueToProcess, int fromRadix)

{

// Convert the input to a string, so we can work through it

// character-by-character

string stringToProcess = valueToProcess.ToString();

int newTotal = 0;

// Loop through from position 0 to the length of the input

for (int index = 0; index < stringToProcess.Length; index++)

{

// Convert each position back into an integer

int currentNumber = int.Parse(stringToProcess.Substring((stringToProcess.Length-1) – index, 1));

// Multiply the current number by the fromRadix^index power

// In other words, for position 3 and a radix of two:

// 1 * (2^3)

newTotal += currentNumber * (int) Math.Pow(fromRadix, index);

}

// Return the converted number, which is now in Base-10

return newTotal;

}

**The Output:**Now that our input is in Base-10 – we just need to convert it to the desired base now. Here is some code for that:

private static string ProcessRadixConversion(int toRadix, int valueToProcess)

{

StringBuilder output = new StringBuilder();

// Keep processing until we run out of things to process

while (valueToProcess > 0)

{

// We need to get the whole number by doing division

int wholeNumber = valueToProcess / toRadix;

// and the remainder by doing "mod"

int remainder = (valueToProcess % toRadix);

// We inject the remainder in the front of the value

output.Insert(0, remainder);

// Then process the whole number that is left.

valueToProcess = wholeNumber;

}

// We’ve processed all the positions, return the converted output.

return output.ToString();

}

So, now we can put it all together, here is an example of some code to use these functions:

int fromBase = 5;

int fromNumber = 12345;

int number = ToBase10(fromNumber, fromBase);

Debug.WriteLine(string.Format("Base {0}: {1}", 2, ProcessRadixConversion(2, number)));

Debug.WriteLine(string.Format("Base {0}: {1}", 3, ProcessRadixConversion(3, number)));

Debug.WriteLine(string.Format("Base {0}: {1}", 4, ProcessRadixConversion(4, number)));

Debug.WriteLine(string.Format("Base {0}: {1}", 5, ProcessRadixConversion(5, number)));

Debug.WriteLine(string.Format("Base {0}: {1}", 6, ProcessRadixConversion(6, number)));

Debug.WriteLine(string.Format("Base {0}: {1}", 7, ProcessRadixConversion(7, number)));

Debug.WriteLine(string.Format("Base {0}: {1}", 8, ProcessRadixConversion(8, number)));

Debug.WriteLine(string.Format("Base {0}: {1}", 9, ProcessRadixConversion(9, number)));

Debug.WriteLine(string.Format("Base {0}: {1}", 10, ProcessRadixConversion(10, number)));

and that outputs:

Base 2: 1111001111

Base 3: 1100010

Base 4: 33033

Base 5: 12400

Base 6: 4303

Base 7: 2562

Base 8: 1717

Base 9: 1303

Base 10: 975

Lastly, we can now tie this back to the original problem at the top of this post and get our answer very quickly:

// 127(10) + 3112(3) – 1001011(2) = ??

int firstNumber = ToBase10(127, 10); // 127

int secondNumber = ToBase10(3112, 3); // 95

int thirdNumber = ToBase10(1001011, 2); // 75

// 127 + 95 – 75 = 147

int answer = firstNumber + secondNumber – thirdNumber;

Debug.WriteLine(string.Format("Base {0}: {1}", 2, ProcessRadixConversion(2, answer)));

Debug.WriteLine(string.Format("Base {0}: {1}", 3, ProcessRadixConversion(3, answer)));

Debug.WriteLine(string.Format("Base {0}: {1}", 4, ProcessRadixConversion(4, answer)));

Debug.WriteLine(string.Format("Base {0}: {1}", 5, ProcessRadixConversion(5, answer)));

Debug.WriteLine(string.Format("Base {0}: {1}", 6, ProcessRadixConversion(6, answer)));

Debug.WriteLine(string.Format("Base {0}: {1}", 7, ProcessRadixConversion(7, answer)));

Debug.WriteLine(string.Format("Base {0}: {1}", 8, ProcessRadixConversion(8, answer)));

Debug.WriteLine(string.Format("Base {0}: {1}", 9, ProcessRadixConversion(9, answer)));

Debug.WriteLine(string.Format("Base {0}: {1}", 10, ProcessRadixConversion(10, answer)));

and the output (and answer, in several bases) is:

Base 2: 10010011

Base 3: 12110

Base 4: 2103

Base 5: 1042

Base 6: 403

Base 7: 300

Base 8: 223

Base 9: 173

Base 10: 147

So there it is. Hope that helps someone or maybe future-me, as I still have more technical classes coming up!

[…] my late-in-life bachelors degree, I needed to be able to calculate between two base numbers. I wrote a whole blog post about it here, if interested. Well, I ran across that code again, cleaned it up and hosted it. More on that in a minute. First, […]

LikeLike