Question

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!

Was it helpful?

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

OTHER TIPS

Try something like this:

For Each c As Char In yourString
    Dim i As Int32 = DirectCast(c, Int32)
Next

Remeber that System.String implements IEnumerable<Char> so it is legal to For Each over it. And converting between a character and a number is as simple as casting between System.Char and System.Int32 (here I have shown how to get the numeric value for each character in the string).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top