Hi I am trying to split a string into an array of chars but for some reason the result is not what I expect.I passed this piece of code threw with the debugger and i gave it the string "34325".When I reach the part of the code that converts the string into an array of chars for some reason I can see two sets of chars in the debugger.I get something like this:
char[0] = 51 '3' char[1] = 52 '4' char[2] = 51 '3' char[3] = 50 '2' char[4] = 53 '5'
When I then convert each element of the char array into an int the first number of is always take : 51 , 52 ,51 , 50 , 53.
My question is how can I correct this so I get 3 , 4 , 3 , 2 , 5? And also from where are this numbers coming from when I use the toCharArray() method : 51 ,52 ,51 ,50 53?
This is my code:
value = TextBox1.Text;
char[] numberChars = value.ToCharArray();
int[] numbers = numberChars.Select(x => Convert.ToInt32(x)).ToArray();
for( int i = 0; i < numbers.Length; i++ ) {
TextBox2.Text += numbers[i] + " ";
}
If I understand your question correctly, you would like to split the string into integer numbers representing the digits, as follows: "34325" becomes new int[] {3,4,3,2,5}.
Change your code as follows to interpret each character that represents a digit as a single-digit number:
int[] numbers = numberChars.Select(x => ((int)x)-'0').ToArray();
Here is a link to a demo on ideone.
The reason you see numbers 51, 52, and so on is that you see ASCII codes for the corresponding digits.
the "other" sequence you got is the sequence of ascii values ...
have a look at int.Parse(string)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With