|
I saw the previous post on this same type of question however it wasn't much help
Here is what I need to do
Start at
A0001... A9999 to B0000...Z9999 to AA000...ZZZZZ
I found this code but it didn't explain what to put in the the variables your passing to it.
string base36Characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string toBase36(int x, int digits)
{
char[] result = new char[digits];
for (int i = digits - 1; i >= 0; --i)
{
result[i] = base36Characters[x % 36];
x /= 36;
}
return new string(result);
}
IEnumerable<string> base36Counter()
{
for (int n = 0; n < 36 * 36 * 36 * 36; ++n)
{
yield return toBase36(n, 4);
}
}
void Run()
{
foreach (string s in base36Counter())
Console.WriteLine(s);
}
|
|
|
|
|
Just split the counter as shown below
string base36Characters = "0123456789";
string base36Alaphs = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string toBase36(int x, int digits)
{
char[] result = new char[digits];
for (int i = digits - 1; i >= 0; --i)
{
result[i] = base36Characters[x % 10];
x /= 10;
}
return new string(result);
}
IEnumerable<string> base36Counter()
{
for (int i = 0; i < base36Alaphs.Length; i++)
{
for (int n = 0; n <; 10 * 10 * 10 * 10; ++n)
{
yield return base36Alaphs[i] + toBase36(n, 4);
}
}
}
void Run()
{
foreach (string s in base36Counter())
Console.WriteLine(s);
}
|
|
|
|
|
This works only for the single alpha character case (A0000...Z9999), it doesn't handle the next numbers in the sequence OP described.
|
|
|
|
|
Oh... Yes... I didnt notice his second set of sequence..
hmmmm.. need to workout!! Thanks for the hint. will update my answer soon.
|
|
|
|
|
This code you're showing does base 36 which is not what you're showing as what you need.
I.e., the code will produce:
0001, 0002, ... 0009, 000A, 000B, ..., 000Z, 0010, 0011, 0012, ..., 0ZZZ, A000, A001, ... ZZZZ I'm not exactly sure how to get the sequence you are describing...
|
|
|
|
|
Do you just need code to generate the strings in that sequence or do you need conversion to/from int?
The implementations likely will be quite different depending on the answer.
(Code based on the code you've posted assumes all you need is the strings in sequence. And never needing to start mid-sequence!)
|
|
|
|
|
I'm not sure I completely understand...
I think it will always be in sequence... It will never jump around..
Im trying to edit some current code in a stored procedure to see if I can make it work.. But no luck yet
|
|
|
|
|
"...in a stored procedure..."
Do you need the answer as C# or as SQL?
|
|
|
|
|
I can do either really... Right before I left work yesterday I think I got something to resemeble working in the stored procedure.
|
|
|
|
|
This looks like a standard Excel column number followed by a four-digit decimal part. Assuming you need the code in C#, something like this should work:
static string ConvertNumber(int number)
{
if (number < 0) throw new ArgumentOutOfRangeException();
int decimalPart;
int columnNumber = Math.DivRem(number, 10000, out decimalPart) + 1;
int length = 5 + (int)Math.Floor(Math.Log(columnNumber, 26));
int resultIndex = length - 1;
char[] result = new char[length];
for (int i = 0; i < 4; i++)
{
int remainder;
decimalPart = Math.DivRem(decimalPart, 10, out remainder);
result[resultIndex] = (char)('0' + remainder);
resultIndex--;
}
while (columnNumber != 0)
{
int remainder = (columnNumber - 1) % 26;
columnNumber = (columnNumber - remainder - 1) / 26;
result[resultIndex] = (char)('A' + remainder);
resultIndex--;
}
resultIndex++;
return new string(result, resultIndex, length - resultIndex);
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
It could be that is what Angela (OP) wants, however what is shown is that after Z9999 is AA000. I.e., as the length of the alpha part of the string gets longer, the numeric part gets shorter. The total string length remains 5.
|
|
|
|