+ 2

Convert without returning ASCII value

Is there a simpler approach to convert a string or character to an integer without getting there ASCII value? https://code.sololearn.com/cb0CcjJFpt9R/?ref=app

28th Jan 2023, 6:12 PM
pullin
pullin - avatar
5 Answers
+ 2
I'm not sure what's going on behind int.Parse but somehow Im thinking it's also using ASCII values and do some secret transformation in the background But it's not being explicit and seems to be much easier https://code.sololearn.com/c5ZmqCpyl9H7/?ref=app
28th Jan 2023, 6:29 PM
Mirielle
Mirielle - avatar
+ 2
You can use casting... string a = "s=8"; for (int i = 0; i < a.Length; i++) Console.WriteLine(a[i]+" : " + (int)a[i]); edit: oh. you need 8 as count then string a = "s=8"; int count = a[2]-48; for (int i = 0; i < count ; i++) Console.WriteLine( a[0] );
28th Jan 2023, 6:29 PM
Jayakrishna 🇮🇳
+ 2
Jayakrishna 🇮🇳 Yeah it was my fault. Sorry for the misunderstanding.
28th Jan 2023, 8:55 PM
pullin
pullin - avatar
+ 2
use Char.GetNumericValue: int count = (int)Char.GetNumericValue(a[2]); or keep it as a string using Substring: int count = int.Parse(a.Substring(2)); or Range Operator: int count = int.Parse(a[2..]);
30th Jan 2023, 12:43 PM
Bob_Li
Bob_Li - avatar
+ 1
Bob_Li It's interesting how the Substring method returns a string even if it only has one character. Thank you for providing more ways.
30th Jan 2023, 3:44 PM
pullin
pullin - avatar