Joel Mueller's answer should guide you the base-64 case.
In response to the preliminary code you've provided in your own answer, you can definitely improve its efficiency by changing the code to accomplish what your for
loop is doing (effectively an O(N) IndexOf
) to use a hash lookup (which should make it O(1)).
I am basing this on the assumption that baseChars
is a field that you initialize in your class's constructor. If this is correct, make the following adjustment:
private Dictionary<char, int> baseChars;
// I don't know what your class is called.
public MultipleBaseNumberFormatter(IEnumerable<char> baseCharacters)
{
// check for baseCharacters != null and Count > 0
baseChars = baseCharacters
.Select((c, i) => new { Value = c, Index = i })
.ToDictionary(x => x.Value, x => x.Index);
}
Then in your StringToInt
method:
char next = encodedString[currentChar];
// No enumerating -- we've gone from O(N) to O(1)!
if (!characterIndices.TryGetValue(next, out nextCharIndex))
{
throw new ArgumentException("Input includes illegal characters.");
}