public static Int64 Decode(string input)
{
var reversed = input.ToLower().Reverse();
long result = 0;
int pos = 0;
foreach (char c in reversed)
{
result += CharList.IndexOf(c) * (long)Math.Pow(36, pos);
pos++;
}
return result;
}
I am using a method to decode a value from base36 to decimal. The method works fine
but when I get to decode the input value of “000A” then things start to go wrong and it
decodes this as -1.
Can anyone see what’s going wrong? I am really confused with the code and how it works.
private const string CharList = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
You’re using
ToLower()on your source and your list contains uppercase chars only, soIndexOf('a')returns -1.I think you’d want to use
ToUpper()instead.