Edit: Updated to answer a clarified question.
The following code will generate the counter that you describe:
0000, 0001 ... 9999, A000 ... A999, B000 ... Z999, ZA00 ... ZA99, ZB00 ... ZZ99, ZZA0 ... ZZZ9, ZZZA ... ZZZZ
public const int MAX_VALUE = 38885;
public static IEnumerable<string> CustomCounter()
{
for (int i = 0; i <= MAX_VALUE; ++i)
yield return Format(i);
}
public static string Format(int i)
{
if (i < 0)
throw new Exception("Negative values not supported.");
if (i > MAX_VALUE)
throw new Exception("Greater than MAX_VALUE");
return String.Format("{0}{1}{2}{3}",
FormatDigit(CalculateDigit(1000, ref i)),
FormatDigit(CalculateDigit(100, ref i)),
FormatDigit(CalculateDigit(10, ref i)),
FormatDigit(i));
}
private static int CalculateDigit(int m, ref int i)
{
var r = i / m;
i = i % m;
if (r > 35)
{
i += (r - 35) * m;
r = 35;
}
return r;
}
private static char FormatDigit(int d)
{
return (char)(d < 10 ? '0' + d : 'A' + d - 10);
}
source
share