Introduction
There are times you have to work with some numbers not from the usual bases (like base10, base2, base16). The easiest way to handle them is to convert to base10, but .NET has no built in solution for that...
I will provide one...
Background
The trigger for this small tip was a QA question, that was solved by different poeple in different ways - none of them statisfied me :-)...
Using the Code
static decimal ToBase10(string Value, string BaseChars)
{
int nBase = BaseChars.Length;
decimal nResult = 0;
int nPos = Value.Length - 1;
foreach (char chDigit in Value.ToUpper().ToArray())
{
nResult += (decimal)Math.Pow(nBase, nPos) * BaseChars.ToUpper().IndexOf(chDigit);
nPos--;
}
return (nResult);
}
This code can be used anywhere in your code, in the most simplest way - call it...
decimal base10 = ToBase10("YYY", "XYZ");
Samples and Explanation
Let's start with the parameters of the method...
Value
- it is the basen value to be converted to base10 BaseChars
- a string
containing the 'digits' - in order - used in the specific base
Note: For a single digit you can use only a single sign, but any sign!
Now let's see some samples...
Base3 in Different Way...
ToBase10("YYY", "XYZ");
This is a base3 number where the 'digits' to use are X, Y, Z (instead of 0, 1, 2 as we would do normally)... So, following the rules of math, the value of this number is:
$3^{0} * Y + 3^{1} * Y + 3^{2} * Y = Y + 3 * Y + 9 * Y = 13 * Y$
Y is the second 'digit' in this sample, so it's true value is 1... So the final resutl is 13!
Base36 - English numerics...
ToBase10("ENGLISH", "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
The base is now 36 (26 letters and 10 digits), so the computation goes like this:
$36^{0} * H + 36^{1} * S + 36^{2} * I + 36^{3} * L + 36^{4} * G + 36^{5} * N + 36^{6} * E = $
$1 * H + 36 * S + 1296 * I + 46656 * L + 1679616 * G + 60466176 * N + 2176782336 * E$
The values of the 'digits' according their position are:
$E = 14, N = 23, G = 16, L = 21, I = 18, S = 28, H = 17$
Inserting these into the equation, you will have 31,893,552,737
as result - a nice number...
Base10 - Can You Recognize?
ToBase10("(!", "!@#$%^&*()");
It is 80
, but I will not explain why...that's for you...
Born in Hungary, got my first computer at age 12 (C64 with tape and joystick). Also got a book with it about 6502 assembly, that on its back has a motto, said 'Try yourself!'. I believe this is my beginning...
Started to learn - formally - in connection to mathematics an physics, by writing basic and assembly programs demoing theorems and experiments.
After moving to Israel learned two years in college and got a software engineering degree, I still have somewhere...
Since 1997 I do development for living. I used 286 assembly, COBOL, C/C++, Magic, Pascal, Visual Basic, C#, JavaScript, HTML, CSS, PHP, ASP, ASP.NET, C# and some more buzzes.
Since 2005 I have to find spare time after kids go bed, which means can't sleep to much, but much happier this way...
Free tools I've created for you...