可以使用標準算法用於從一個int轉換爲base36字符串,通過取基體的模,然後通過鹼將所述餘數提取每次一個數位,但是添加第一個和最後一個數字的特殊情況:
對於例如在Java中:
static String getId(int id)
{
String s = "";
for(int i = 0; i < 6; i++)
{
// compute the digit using modulo arithmetic using base 26
// for first and last character and base 36 for others
int digit;
if((i == 0) || (i == 5))
{
digit = (id % 26) + 10;
id /= 26;
}
else
{
digit = id % 36;
id /= 36;
}
// add the digit to the string:
if(digit < 10)
s = (char)('0' + digit) + s;
else
s = (char)('A' + (digit - 10)) + s;
}
return s;
}
有26 * 36 * 36 * 36 * 36 * 26 = 1135420416的可能性,這意味着你只需要一個32位整型來存儲他們所有。
所以你有26^2 * 36^4的可能性。您只需要一個轉換器,將數字從0轉換爲[A-Z](0-9A-Z){4} [A-Z]類型的序列。類似於d0 = n%26,d1 = n/26%36; d2 = n/26 * 36%36 ...應該很簡單... – CFrei