I'm prefilling a byte array with:
private static byte[] idBuffer = ASCIIEncoding.Default.GetBytes("A" + DateTime.Now.ToString("yy") + DateTime.Now.DayOfYear + "0000001");
The "0000001" part in the byte array is my ID part that I would like to increment with ASCII characters 0-1A-S every time I call the "Increment" method.
For example, increment sequence samples would be:
000000S
...
00000S0
00000S1
...
00000SS
...
0000S00
...
0000S99
0000S9A
etc.
I'm having some trouble coming up with the correct algorithm/state machine to increment the characters correctly.
Right now I prefill a char table:
private static byte[] charCodes = new byte[] { 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, 80, 81, 82, 83};
and my crude attempt at the state machine for this but it only gets me to the 2nd position:
if (idBuffer[bufPosition] < 83)
{
idBuffer[bufPosition] = charCodes[charCodePosition];
charCodePosition++;
}
else
{
if (idBuffer[bufPosition - 1] == 48)
{
for (int i = bufPosition; i < idBuffer.Length; i++)
idBuffer[i] = 48;
idBuffer[bufPosition - 1] = charCodes[charCodePosition2];
charCodePosition2++;
}
else
{
charCodePosition2++;
idBuffer[bufPosition - 1] = charCodes[charCodePosition2];
}
charCodePosition = 0;
}
I'm sure there is a much more elegant way to do this that I can't think of - Ideally if someone knows how to do this with unsafe code/pointers, even better!