Click here to Skip to main content
15,921,371 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.5K   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 
AlphaNumeric.vb class:

Imports System.Collections.Generic
Imports System.Text

Namespace INCREMENT_IN_C_SHARP
    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
    End Enum

    Public NotInheritable Class AlphaNumeric
        Private Sub New()
        End Sub
        Private Shared aSCIIValues As Byte()
        Private Shared indexToCheck As Integer

        Private Shared m_requiredLength As Integer = 2
        Public Shared Property RequiredLength() As Integer
            Get
                Return m_requiredLength
            End Get
            Set(ByVal value As Integer)
                m_requiredLength = value
            End Set
        End Property

        Public Shared Function NextKeyCode(ByVal KeyCode As String, ByVal Sequence As SequenceType) As String
            If KeyCode.Length <> m_requiredLength Then
                Select Case Sequence
                    Case SequenceType.NumericToAlpha
                        Return MakeCustomLengthString("0", m_requiredLength)
                    Case SequenceType.AlphaToNumeric
                        Return MakeCustomLengthString("A", m_requiredLength)
                    Case SequenceType.AlphaNumeric
                        Return "A" & MakeCustomLengthString("0", m_requiredLength - 1)
                    Case SequenceType.NumericOnly
                        Return MakeCustomLengthString("0", m_requiredLength)
                    Case SequenceType.AlphaOnly
                        Return MakeCustomLengthString("A", m_requiredLength)
                    Case Else
                        Return ""
                End Select
            End If

            'If reached to max
            Select Case Sequence
                Case SequenceType.NumericToAlpha
                    If KeyCode = MakeCustomLengthString("Z", m_requiredLength) Then
                        Throw New OverflowException("Maximum number is reached")
                    End If
                    Exit Select
                Case SequenceType.AlphaToNumeric
                    If KeyCode = MakeCustomLengthString("9", m_requiredLength) Then
                        Throw New OverflowException("Maximum number is reached")
                    End If
                    Exit Select
                Case SequenceType.AlphaNumeric
                    If KeyCode = MakeCustomLengthString("9", m_requiredLength) Then
                        Throw New OverflowException("Maximum number is reached")
                    End If
                    Exit Select
                Case SequenceType.NumericOnly
                    If KeyCode = MakeCustomLengthString("9", m_requiredLength) Then
                        Throw New OverflowException("Maximum number is reached")
                    End If
                    Exit Select
                Case SequenceType.AlphaOnly
                    If KeyCode = MakeCustomLengthString("Z", m_requiredLength) Then
                        Throw New OverflowException("Maximum number is reached")
                    End If
                    Exit Select
                Case Else
                    Exit Select
            End Select

            aSCIIValues = ASCIIEncoding.ASCII.GetBytes(KeyCode.ToUpper())

            indexToCheck = aSCIIValues.Length - 1
            Dim keepChecking As Boolean = True
            While keepChecking
                aSCIIValues(indexToCheck) = [next](aSCIIValues(indexToCheck), Sequence)
                If aSCIIValues(indexToCheck) = SingleCharacterMaxValue(Sequence) AndAlso indexToCheck <> 0 Then
                    indexToCheck -= 1
                Else
                    keepChecking = False
                End If
            End While

            KeyCode = ASCIIEncoding.ASCII.GetString(aSCIIValues)
            Return KeyCode
        End Function

        Private Shared Function [next](ByVal current As Integer, ByVal sequence As SequenceType) As Byte
            Select Case sequence
                Case SequenceType.NumericToAlpha
                    If current = 57 Then
                        current = 65
                    ElseIf current = 90 Then
                        current = 48
                    Else
                        current += 1
                    End If
                    Exit Select
                Case SequenceType.AlphaToNumeric
                    If current = 90 Then
                        current = 48
                    ElseIf current = 57 Then
                        current = 65
                    Else
                        current += 1
                    End If
                    Exit Select
                Case SequenceType.AlphaNumeric
                    If current = 57 Then
                        current = 65
                    ElseIf current = 90 Then
                        current = 48
                    Else
                        current += 1
                    End If
                    Exit Select
                Case SequenceType.NumericOnly
                    If current = 57 Then
                        current = 48
                    Else
                        current += 1
                    End If
                    Exit Select
                Case SequenceType.AlphaOnly
                    If current = 90 Then
                        current = 65
                    Else
                        current += 1
                    End If
                    Exit Select
                Case Else
                    Exit Select
            End Select

            Return Convert.ToByte(current)
        End Function

        Private Shared Function MakeCustomLengthString(ByVal data As String, ByVal length As Integer) As String
            Dim result As String = ""
            For i As Integer = 1 To length
                result += data
            Next

            Return result
        End Function

        Private Shared Function SingleCharacterMaxValue(ByVal sequence As SequenceType) As Integer
            Dim result As Integer = 0
            Select Case sequence
                Case SequenceType.NumericToAlpha
                    result = 48
                    Exit Select
                Case SequenceType.AlphaToNumeric
                    result = 65
                    Exit Select
                Case SequenceType.AlphaNumeric
                    result = 48
                    Exit Select
                Case SequenceType.NumericOnly
                    result = 48
                    Exit Select
                Case SequenceType.AlphaOnly
                    result = 65
                    Exit Select
                Case Else
                    Exit Select
            End Select

            Return result
        End Function

        Private Shared Function isAllNine() As Boolean
            Dim result As Boolean = True
            For i As Integer = 0 To aSCIIValues.Length - 1
                If aSCIIValues(i) <> 57 Then
                    result = False
                    Exit For
                End If
            Next
            Return result
        End Function

    End Class
End Namespace




Form1 code:

Imports System.Drawing
Imports System.Collections
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Data
Imports System.Text
Imports alphanumeric_increment.INCREMENT_IN_C_SHARP

Public Class Form1
    <STAThread()> _
  Private Shared Sub Main()
        Application.Run(New Form1())
    End Sub

    Private sequenceType As SequenceType = sequenceType.NumericOnly

    Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles button1.Click
        labelMessage.Text = ""
        Try
            AlphaNumeric.RequiredLength = Convert.ToInt32(textBox3.Text)

            textBox1.Text = AlphaNumeric.NextKeyCode(textBox1.Text, sequenceType)
            listBox1.Items.Insert(0, textBox1.Text)
            textBox2.Text = (Convert.ToInt32(textBox2.Text) + 1).ToString()
        Catch oex As OverflowException
            labelMessage.Text = oex.Message
        Catch ex As Exception
            labelMessage.Text = ex.Message
        End Try
    End Sub


    Private Sub radioNumericToAlpha_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles radioNumericToAlpha.CheckedChanged
        If radioNumericToAlpha.Checked Then
            sequenceType = sequenceType.NumericToAlpha
            textBox1.Text = "0"
            textBox2.Text = "0"
            listBox1.Items.Clear()
            labelSample.Text = "00,01,...,09,0A,...0Z,10,11...,A0,A1,...,ZZ"
        End If
    End Sub

    Private Sub radioAlphaToNumeric_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles radioAlphaToNumeric.CheckedChanged
        If radioAlphaToNumeric.Checked Then
            sequenceType = sequenceType.AlphaToNumeric
            textBox1.Text = "0"
            textBox2.Text = "0"
            listBox1.Items.Clear()
            labelSample.Text = "AA,AB,...,AZ,A0,...A9,BA,BB...ZZ,00,01,...99"
        End If
    End Sub

    Private Sub radioAlphaNumeric_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles radioAlphaNumeric.CheckedChanged
        If radioAlphaNumeric.Checked Then
            sequenceType = sequenceType.AlphaNumeric
            textBox1.Text = "0"
            textBox2.Text = "0"
            listBox1.Items.Clear()
            labelSample.Text = "A0,A1,...,A9,AA,...AZ,B0,B1...ZZ,00,01,...99"
        End If
    End Sub

    Private Sub radioNumericOnly_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles radioNumericOnly.CheckedChanged
        If radioNumericOnly.Checked Then
            sequenceType = sequenceType.NumericOnly
            textBox1.Text = "0"
            textBox2.Text = "0"
            listBox1.Items.Clear()
            labelSample.Text = "00,01,...99"
        End If
    End Sub

    Private Sub radioAlphaOnly_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles radioAlphaOnly.CheckedChanged
        If radioAlphaOnly.Checked Then
            sequenceType = sequenceType.AlphaOnly
            textBox1.Text = "0"
            textBox2.Text = "0"
            listBox1.Items.Clear()
            labelSample.Text = "AA,AB,...,ZZ"
        End If
    End Sub


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

 
End Class

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.