您可以通過將數字轉換爲Base36來實現此目的。
看看這個例子:
private const string CharList = "abcdefghijklmnopqrstuvwxyz";
public static String Base36Encode(long input, char paddingChar, int totalWidth)
{
char[] clistarr = CharList.ToCharArray();
var result = new Stack<char>();
while (input != 0)
{
result.Push(clistarr[input % 36]);
input /= 36;
}
return new string(result.ToArray()).PadLeft(totalWidth, paddingChar).ToUpper();
}
,然後使用這種方式:
for(int i = 0; i < 1000; i++)
{
Debug.WriteLine(Base36Encode(i, '0', 6));
}
這將產生這樣:
000000,000001,000002 ,000003,000004,000005,000006,000007,000008,000009,00000A,00000B,00000C,00000D,00000E,00000F ,00000G,00000H,00000I,00000J,00000K,00000L,00000M,00000N,00000O,00000P,00000Q,00000R,00000S,00000T,00000U,00000V,00000W,00000X,00000Y,00000Z,000010,000011,000012,000013, ,000015,000016,000017,000018,000019,00001A,00001B,00001C,00001D,00001E,00001F,00001G,00001H,00001I,00001J,00001K,00001L,00001M,00001N,00001O,00001P,00001Q,00001R,00001S,00001T ,00001U,00001V,00001W,00001X,00001Y,00001Z,000020,000021,000022,000023,000024,000025,000026,000027,000028,000029,00002A,00002B,00002C,00002D,00002E,00002F,00002G,00002H,00002I ,00002J,00002K,00002L,00002M,00002N,00002O,00002P,00002Q,00002R,00002S,00002T ...
和對這種做法的積極的事情是,你可以通過這個轉換回數字:
public static Int64 Base36Decode(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;
}
您可以有一個解析器和串組合,或者你可以維持目前的數量(在這種情況下,你只需要ToString方法)。無論哪種方式,我會分開數和字符串之間的轉換,做別的 –
如果一切正常,你只是想使其更快,然後使用一個分析器,以確定哪一塊是最慢的和工作。否則,你只是猜測。 –