Convert a String to a Number


How can i convert a character to a number, so that the result is only numerical digits, and would always be the same, besides 26 (plus non-alpha characters) if-then statements.
like, A = 1 and A always equals 1
so if I had the string:  1234A
after converting, I would get the result:  12341
A doesn't necessarily have to = 1, or even a single digit.  It just has to equal a numerical... number.
0
bortiquai
12/29/2007 12:26:03 AM
📁 asp.net.web-forms
📃 93655 articles.
⭐ 6 followers.

💬 4 Replies
👁️‍🗨️ 901 Views



perhaps you can do something like this. 
Store your letters in an array A and maintain a cooresponding array B that has the numeric equivalent you would want to replace the letter with.
When an alpha character is found in array A, you simply substitute the value from array B at the same index position giving you repeatable replacements.
        Dim arA As Char() = {"A", "B", "C", "D", "E"}
        Dim arB As Char() = {"1", "2", "3", "4", "5"}

        Dim test As String = "1234A"

        'convert test string into a char array
        Dim chars As Char() = test.ToCharArray

        'for each char in our input string
        For index As Integer = 0 To chars.Length - 1
            'find each char in the first array
            'converting to uppercase as we go
            'to match how we entered them in our 1st array
            Dim i As Integer = Array.IndexOf(arA, Char.ToUpperInvariant(chars(index)))

            'if found, use the char in the same index position from the 2nd array
            'and replace the character
            If i >= 0 Then chars(index) = arB(i)

        Next

        'now convert the char array back into a string
        test = New String(chars)

        'test will now equal "12341"
      

Mike Banavige
~~~~~~~~~~~~

Need a site code sample in a different language? Try converting it with: http://converter.telerik.com/
0
mbanavige
12/29/2007 1:00:04 AM

Yeah, that would work.  But i was looking for something even simpler, like using an ASCII code, or converting the string as a whole to hex.
 Like ConvertToSomething(125t-12A) = 23098432 or whatever.
 Maybe i could combine that with your idea, but instead of using arrays, just iterate each character and if it is non-numeric, convert it to something that is numeric.
 Thanks for your suggestions.
0
bortiquai
12/29/2007 1:15:41 AM

Try something like this:
 string x = "50";
Int32 i = Int32.Parse(x);

 Please remember to Mark this post as Answered if it serves your purpose so that issue can be closed and others can refer.
0
SKVSKV
12/29/2007 1:19:24 AM

bortiquai:

... just iterate each character and if it is non-numeric, convert it to something that is numeric.


In order to distinguish between the characters that are numeric digits and those that are not, i think you will need to iterate.  Here's a simpler iteration.
        Dim sb As New StringBuilder
        Dim test As String = "1234A"
        For Each c As Char In test.ToCharArray
            If Char.IsDigit(c) Then
                sb.Append(c)
            Else
                sb.Append(Asc(c))
            End If
        Next
        test = sb.ToString
 

Mike Banavige
~~~~~~~~~~~~

Need a site code sample in a different language? Try converting it with: http://converter.telerik.com/
0
mbanavige
12/29/2007 1:25:21 AM
Reply: