Click here to Skip to main content
15,892,746 members
Articles / Programming Languages / C#

AlphaNumeric Increment

Rate me:
Please Sign up or sign in to vote.
4.65/5 (13 votes)
11 Mar 2009CPOL 51.2K   1.8K   20   7
AlphaNumeric series Increment

Introduction 

Image 1

Alpha numeric series generator.

Background  

Many alpha numeric samples increment only numeric part and append alpha as prefix, but the requirement was to increment all characters in a series. 

This is usually required if you want to generate some unique varchar value for a database, or to achieve more combination in short length.

  • Numeric 0-9 (Length 2)       = 100 combinations
  • Numeric A-Z (Length 2)       = 676 combinations 
  • Numeric 0-9|A-Z (Length 2) = 1296 combinations

Also it should have modes to control series generation direction.

Using the Code

Series sequence types are as follows. You may control the direction i.e. numeric first, alpha first or mix modes... 

The following sample is for 2 character length. 

NumericToAlpha, AlphaToNumeric and AlphaNumeric will generate the same result but the generation sequence will be different.

C#
public enum SequenceType
    {
        /// <summary>
        /// 00,01,...,09,0A,...0Z,10,11...,A0,A1,...,ZZ
        /// </summary>
        NumericToAlpha = 1,
        /// <summary>
        /// AA,AB,...,AZ,A0,...A9,BA,BB...ZZ,00,01,...99
        /// </summary>
        AlphaToNumeric = 2,
        /// <summary>
        /// A0,A1,...,A9,AA,...AZ,B0,B1...ZZ,00,01,...99
        /// </summary>
        AlphaNumeric = 3,
        /// <summary>
        /// 00,01,...99
        /// </summary>
        NumericOnly = 4,
        /// <summary>
        /// AA,AB,...,ZZ
        /// </summary>
        AlphaOnly = 5
    }

     while (keepChecking)
         {...

This will convert all to ASCII and iterate on all ASCII values and increments each, while it keeps track to toggle the next ASCII if it first reaches its maximum defined value.

C#
aSCIIValues = ASCIIEncoding.ASCII.GetBytes(KeyCode.ToUpper());

indexToCheck = aSCIIValues.Length - 1;
bool keepChecking = true;
while (keepChecking)
{
        aSCIIValues[indexToCheck] = next(aSCIIValues[indexToCheck], Sequence);
        if (aSCIIValues[indexToCheck] ==
    SingleCharacterMaxValue(Sequence) && indexToCheck != 0)
            indexToCheck--;
        else
           keepChecking = false;
}

KeyCode = ASCIIEncoding.ASCII.GetString(aSCIIValues);

This will return the next ASCII value as per sequence type.

C#
private static byte next(int current, SequenceType sequence)
        {
            switch (sequence)
            {
                case SequenceType.NumericToAlpha:
                    if (current == 57)
                        current = 65;
                    else if (current == 90)
                        current = 48;
                    else
                        current++;
                    break;
                case SequenceType.AlphaToNumeric:
                    if (current == 90)
                        current = 48;
                    else if (current == 57)
                        current = 65;
                    else
                        current++;
                    break;
                case SequenceType.AlphaNumeric:
                    if (current == 57)
                        current = 65;
                    else if (current == 90)
                        current = 48;
                    else
                        current++;
                    break;
                case SequenceType.NumericOnly:
                    if (current == 57)
                        current = 48;
                    else
                        current++;
                    break;
                case SequenceType.AlphaOnly:
                    if (current == 90)
                        current = 65;
                    else
                        current++;
                    break;
                default:
                    break;
            }

            return Convert.ToByte(current);
        } 

License

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


Written By
Software Developer Visualsoft-Inc
Pakistan Pakistan
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralHere i posted vb.net code of this project. Pin
la_morte1-Feb-11 21:28
la_morte1-Feb-11 21:28 
GeneralAlternative alpha sequences Pin
supercat912-Mar-09 5:31
supercat912-Mar-09 5:31 
GeneralGood Pin
Ozerich12-Mar-09 4:48
Ozerich12-Mar-09 4:48 
GeneralGood Article Pin
ridda78611-Mar-09 23:47
ridda78611-Mar-09 23:47 
GeneralCool man! Pin
ch3ckmat311-Mar-09 23:14
ch3ckmat311-Mar-09 23:14 
GeneralAnd Pin
PIEBALDconsult11-Mar-09 5:28
mvePIEBALDconsult11-Mar-09 5:28 
GeneralThoughts Pin
PIEBALDconsult11-Mar-09 5:22
mvePIEBALDconsult11-Mar-09 5:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.