How I can do convert int to string to char to int correctly ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How I can do convert int to string to char to int correctly ?

I want to have fnc that load number n and covert int to string t. because I want to us foeach (char x in t). bud my code will never end. because I have problem with converting. there is my problem in short code. int n = 101; string t = Convert.ToString(n); char x = t[1]; int m = Convert.ToInt32(x); Console.WriteLine(m); result != 0 Why ? and How can I fix it ?

22nd Dec 2017, 1:44 AM
Filip Dobeš
1 Answer
0
when you try to convert the char 0(index 1 of n) into an integer, it will give you the corresponding value of itself in the ASCII table, and not the actual numeric value of the string. Here's your solution: int m = (int)char.GetNumerucValue(x); This is going to give you, as the method's name says, the NUMERIC VALUE of the CHAR that you are trying to convert. I hope this is what you were looking for :)
22nd Dec 2017, 3:05 AM
Welliton Malta
Welliton Malta - avatar