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
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
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-48; for (int i = 0; i < count ; i++) Console.WriteLine( a );
Jayakrishna 🇮🇳 Yeah it was my fault. Sorry for the misunderstanding.
use Char.GetNumericValue: int count = (int)Char.GetNumericValue(a); or keep it as a string using Substring: int count = int.Parse(a.Substring(2)); or Range Operator: int count = int.Parse(a[2..]);
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.