Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I am looking for a script that able to generate a serialized running character that looks like as below

AAAA,AAAB,AAAC,AAAD and when it reach AAAZ, it will auto round up to AABA and continue the incremental.

What I have tried:

Until now, I have no clue how should start
Posted
Updated 7-Aug-19 0:38am

The way I would do it is to store it as a number - 0 to (26^4 - 1) : 456975 and write a conversion method:
First char is (value divided by 26^3) + 'A'
Second is     (value divided by 26^2 Modulus 26) + 'A'
Third is      (value divided by 26 Modulus 26) + 'A'
Fourth is     (value Modulus 26) + 'A'
Then all you need to do is increment the integer, and convert it to a string when you want to present it to the user. That also makes it a lot easier to cope when you reach ZZZZ and have to move to a five character scheme: it's a single change to one method instead of changes to loads of places.
 
Share this answer
 
Comments
Maciej Los 7-Aug-19 8:29am    
Genius idea!
The question is why below linq query returns wrong results:
int constval = 26;
List<int> values = Enumerable.Range(0,255).ToList();

var aphabet = values
	.Select(x=>new {
		Init = x,
		Key = string.Concat((char)((x/constval^3) + 'A'),
			(char)((x/constval^2 % constval) + 'A'),
			(char)((x/constval % constval) + 'A'),
			(char)((x % constval) + 'A'))
		})
	.ToList();

Firth and fourth chaacter is OK, but the rest is wrong...
Do i made something wrong?
OriginalGriff 7-Aug-19 8:46am    
Operator precedence and forgetfullness? "^" is XOR in C#, not "Power Of". It's "Power Of" in VB.
Try this:
            var alphabet = values
                .Select(x => new {
                    Init = x, 
                    Key = string.Concat((char)((x / (constval * constval * constval)) + 'A'),
                        (char)(((x / (constval * constval)) % constval) + 'A'),
                        (char)(((x / constval) % constval) + 'A'),
                        (char)((x % constval) + 'A'))
                    })
                .ToList();
Maciej Los 7-Aug-19 9:01am    
So stupid mistake! Thanks, OriginalGriff.
OriginalGriff 7-Aug-19 9:09am    
We've all done it!
Maciej Los 7-Aug-19 13:23pm    
Seems, we forgot about Math.Pow method ;)
            var alphabet = values
                .Select(x => new {
                    Init = x, 
                    Key = string.Concat((char)((x / Math.Pow(constval, 3)) + 'A'),
                        (char)(((x / Math.Pow(constval, 2) % constval) + 'A'),
                        (char)(((x / constval) % constval) + 'A'),
                        (char)((x % constval) + 'A'))
                    })
                .ToList();
You might make a class for this, providing a next method (similar to what Random provides). You might even go recursive.
C#
class Seq
  {
    char[] c;
    public Seq()
    {
      c = new char[] { 'A', 'A', 'A', 'A' };
    }
    public string next()
    {
      string result = new string(c);
      go_next(c.Count() -1 );
      return result;
    }

    private void go_next(int index)
    {
      if (index < 0) return;
      if (c[index] < 'Z')
        ++c[index];
      else
      {
        c[index] = 'A';
        go_next(index - 1);
      }
    }
  }
 
Share this answer
 
Comments
Maciej Los 7-Aug-19 13:38pm    
Carlo, probably your class is invalid, because calling next() method changes nothing.
Can you check your class?

[EDIT]
Sorry, my bad (second time this day).
Very good idea!
CPallini 7-Aug-19 15:28pm    
:-) Thank you, Maciej!
I would prefer another way as OriginalGriff :
Your Source is a String which contains the 4 Chars.
Now you could get the ASCII-Code from each Char of this String.
'A' hast the ASCII-Code 65 (thats it's Byte-Value) and 'Z' has the Code 90.
Now you increase the Byte-Value of the lowest Char - if it becomes a value greater 90 you set it back to 65 and increase the next Char-Value. Here you have to do the same check (and so on with the other Chars).
At the end you rebuild the String from the 4 Char-Codes again.

ASC gives you the Char-Code
CHR creates the Char from a Char-Code
 
Share this answer
 
Comments
Teoh Chia Wei 7-Aug-19 4:22am    
do you think you can provide the source code?
Ralf Meier 7-Aug-19 4:32am    
not actually because I haven't a Visual Studio access at the moment.
But I think with my description it should be possible for you to create the code by yourself - give it a try ...
If not you have to wait some hours ...
Quote:
Until now, I have no clue how should start

Looks like you need to learn programming, because even with very basic knowledge, you should be able to get started.

- when something have similarities from the outside, chances are that it also work similarly from the inside.
Quote:
AAAA,AAAB,AAAC,AAAD and when it reach AAAZ, it will auto round up to AABA and continue the incremental.

This is counting with letters instead of digits.
VB
For Counter = 0 to 27
    MyString= ConvertToString(Counter)
    // do something
Next


In number conversion, modulo allow to extract the unit of a number
VB
MyUnit = MyValue Mod 10
MyRemainder = MyValue \ 10

Repeat 4 times to get 4 digits/letters

Say that 0 is 'A', 1 is 'B' ...
you need to convert each unit to letter.

Now, switch to base 26
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900