vb.netstringstringcollection

String.ToCharArray() stored in StringCollection conversion to numbers and back in VB.net


I have a string which was converted to a char array and stored in a stringcollection, how do I return the numerical value for the character in each string? How can I reverse this process, getting the character from the numerical value?

I know I could code a Select Case statement, but that would take a very long time as I need to cover every character a person could want to conceivably use in the English language, including punctuation.

Is there already a method built into vb.net for doing this?

Thanks for the help!


Solution

  • The Convert class has methods that can convert between characters and integers:

    Dim c As Char = "A"C
    Dim i As Integer = Convert.ToInt32(c)
    Dim c2 As Char = Convert.ToChar(i)
    

    To loop the values converted from the characters in a string into an array of integers:

    Dim codes(theString.Length - 1) As Integer
    For i As Integer = 0 to theString.Length - 1
        codes(i) = Convert.ToInt32(theString.Chars(i))
    Next