Convert without returning ASCII value | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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
blank
4 Answers
+ 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
blank
+ 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
blank