views:

397

answers:

2

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!

+1  A: 

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).

Andrew Hare
So I is my returned integer for that single character?
Cyclone
Yes, you are correct :)
Andrew Hare
Okay lol, I will give this a shot.
Cyclone
It did not work, I got"'Char' values cannot be converted to 'Integer'. Use 'Microsoft.VisualBasic.AscW' to interpret a character as a Unicode value or 'Microsoft.VisualBasic.Val' to interpret it as a digit."
Cyclone
Hmm... VB.NET apparently doesn't allow this - C# allows you to cast from `Char` to `Int32`. You will need to use the `Convert` methods that Guffa mentions to do the conversions.
Andrew Hare
Okay then lol...
Cyclone
+2  A: 

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
Guffa
What is that extra c in the first line of code?
Cyclone
@Cyclone: That is a character literal. "A" is a string literal containing one character, and "A"C is a character literal.
Guffa
Okay, so I need to have the character literal if I want this to work? Could I convert a whole string at once?
Cyclone
If you want to get the character for each character in a string, you will have to loop the string and convert each character. There is no built in method to convert a string to an array of integers.
Guffa
Err, okay, if each character is not specifically mentioned as a string, like your first line, how do I declare it as a char literal? Meaning, I have a variable which is holding the value.
Cyclone
Since I didnt have it as a literal, here is my code: For Each letter As Char In Text Output = (Convert.ToInt32(letter)) Next
Cyclone
That failed, I printed out the result and it epicly failed.
Cyclone
As you are storing all the values in the same variable they will overwrite each other, so you only get the code of the last character. Declare an array with the same length as the string, and put the result from converting each character in an element in the array.
Guffa
How do I add each character to an array? I have only used StringCollection and ArrayList before.
Cyclone
I added that to the answer.
Guffa