none
How to conver all alphabet A,B,C... to 1,2 ,3 ... using c# logic? RRS feed

  • Question

  • Hi all,

    I want to covert the A to 1 and B to 2 and so on upto Z so that when user will press A it should save 1 against it.

    Please help me to create logic on it.

    For example  if i type "John" it should return me
      j=10
      0=15
      h=8
      n=14

     "1015814"

    Thanks,
    Varun Kumar
    • Edited by varun007 Monday, January 2, 2012 7:15 AM
    Monday, January 2, 2012 7:07 AM

Answers

  • varun007,

    by using your example you will get into trouble by reverting the numbers to characters.

    "1015814" can also be interpreted like:

    10=j
    1=a
    5=e
    8=h
    1=a
    4=d

    so you should "store" the value of each character by two numbers so your example "john" would result in: "10150814"


    'Visual Basic 2008 - .net 3.5 - Any CPU
        Private Alphabet As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        Private Function getNumericValueInAlphabet(ByVal value As String) As String
    
            ValidateInput(value)
    
            Dim RV As New System.Text.StringBuilder
    
            value = value.ToUpper
    
            For i As Integer = 0 To value.Length - 1
                Dim Index As Integer = Alphabet.IndexOf(value.Chars(i))
                If Index > -1 Then
                    RV.Append((Index + 1).ToString.PadLeft(2, "0"c))
                Else
                    Throw New ArgumentOutOfRangeException("The provided String is not inside then range")
                End If
            Next
    
            Return RV.ToString
    
        End Function
        Private Sub ValidateInput(ByVal value As String)
    
            If value Is Nothing Then
                Throw New ArgumentNullException
            End If
    
            If String.IsNullOrEmpty(value.Trim) Then
                Throw New ArgumentException("The provided String argument is empty")
            End If
    
        End Sub
    

     


    Hannes

    If you have got questions about this, just ask.

    In a perfect world,
    users would never enter data in the wrong form,
    files they choose to open would always exist
    and code would never have bugs.

    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/
    • Marked as answer by varun007 Monday, January 2, 2012 9:06 AM
    Monday, January 2, 2012 8:11 AM

All replies

  • Hi declare,

    enum alphNumeric

    {

    A=1,

    z=26

    }

     


    By Sanz If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".
    • Proposed as answer by san Sanz Monday, January 2, 2012 7:17 AM
    Monday, January 2, 2012 7:17 AM
  • Please help me by giving some example.

     


    Varun Kumar
    Monday, January 2, 2012 7:21 AM
  • Hi varun ,

                 For your reference follow this thread

    http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/78e75d1a-0795-4bdb-8a62-ae6faa909986/

    If you need code just ask.


    If a post answers your question, please click Mark As Answer on that post and Mark as Helpful. Happy Coding...

    • Edited by Ragu.R Monday, January 2, 2012 7:23 AM
    • Proposed as answer by Tamer Oz Monday, January 2, 2012 7:24 AM
    Monday, January 2, 2012 7:23 AM
  • hi Varun ,

    let me know any issues


    By Sanz If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".
    Monday, January 2, 2012 7:40 AM
  • Hi,
     Try this VB code

    'Alphabets Custom Value conversion using ASCII Values
    
    'for small letter set the flag isCaps as 'False'.
    Private Function String2Number (ByVal str As String, ByVal isCaps As Boolean) As String
    Dim iAsciiValue As Integer = Asc(str) ' Ascii value of concerned character
    Dim iPos As Integer = If(isCaps, iAsciiValue - 64, iAsciiValue - 96) ' Our custom conversion like A-1,B-2
    Return iPos.ToString 
    End Function



     


    If a post answers your question, please click Mark As Answer on that post and Mark as Helpful. Happy Coding...

    • Edited by Ragu.R Monday, January 2, 2012 9:18 AM
    Monday, January 2, 2012 7:45 AM
  • varun007,

    by using your example you will get into trouble by reverting the numbers to characters.

    "1015814" can also be interpreted like:

    10=j
    1=a
    5=e
    8=h
    1=a
    4=d

    so you should "store" the value of each character by two numbers so your example "john" would result in: "10150814"


    'Visual Basic 2008 - .net 3.5 - Any CPU
        Private Alphabet As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        Private Function getNumericValueInAlphabet(ByVal value As String) As String
    
            ValidateInput(value)
    
            Dim RV As New System.Text.StringBuilder
    
            value = value.ToUpper
    
            For i As Integer = 0 To value.Length - 1
                Dim Index As Integer = Alphabet.IndexOf(value.Chars(i))
                If Index > -1 Then
                    RV.Append((Index + 1).ToString.PadLeft(2, "0"c))
                Else
                    Throw New ArgumentOutOfRangeException("The provided String is not inside then range")
                End If
            Next
    
            Return RV.ToString
    
        End Function
        Private Sub ValidateInput(ByVal value As String)
    
            If value Is Nothing Then
                Throw New ArgumentNullException
            End If
    
            If String.IsNullOrEmpty(value.Trim) Then
                Throw New ArgumentException("The provided String argument is empty")
            End If
    
        End Sub
    

     


    Hannes

    If you have got questions about this, just ask.

    In a perfect world,
    users would never enter data in the wrong form,
    files they choose to open would always exist
    and code would never have bugs.

    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/
    • Marked as answer by varun007 Monday, January 2, 2012 9:06 AM
    Monday, January 2, 2012 8:11 AM