我使用以下类方法从数字创建Base36字符串:
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());
}
Run Code Online (Sandbox Code Playgroud)
其中一个要求是字符串应始终用零填充,并且最多应为四位数.任何人都可以提出一种方法,我可以编码领先的零,并限制功能,所以它永远不会返回超过"ZZZZ"?C#中是否有一些功能可以做到这一点.对于此代码的缩进感到抱歉.我不确定为什么它没有正确缩进.