This code snippet helps you to understand how to convert char into an int in C# (C sharp).
Using Char.GetNumericValue Method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
using System; namespace SampleApp { class ConvertChartoInt { public static void Main() { char chr = '2'; int integerValue = (int)Char.GetNumericValue(chr); Console.WriteLine(integerValue); Console.ReadKey(); } } } |
Output
1 2 3 |
2 |
Using Int32.Parse Method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
using System; namespace SampleApp { class ConvertChartoInt { public static void Main() { char chr = '2'; int integerValue = int.Parse(chr.ToString()); Console.WriteLine(integerValue); Console.ReadKey(); } } } |