I am using the following class method to create a Base36 string from a number:
private const string CharList = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
public static String Encode(long input) {
if (input < 0) throw new ArgumentOutOfRangeException("input", input, "input cannot be negative");
char[] clistarr = CharList.ToCharArray();
var result = new Stack<char>();
while (input != 0)
{
result.Push(clistarr[input % 36]);
input /= 36;
}
return new string(result.ToArray());
}
One of the requirements is that the string should always be padded with zero’s and should be a maximum of four digits. Can anyone suggest a way that I can code the leading zero’s and also limit the function so it will never return more than “ZZZZ” ? Is there some function in C# that can do this. Sorry about the indentation on this code. I am not sure why it’s not indenting properly.
If there are always going to be exactly four digits, it’s really easy:
Or using a loop: