Characters can be converted to an Int32
type in a simple way. An Int32
datatype means a 32-bit signed integer representation that can be negative or positive.
Convert.ToInt32(character)
character
: This is the character we want to convert to a 32-bit signed integer.
It returns a 32-bit signed integer that is the equivalent of the character.
Let's look at the code below:
using System;class HelloWorld{static void Main(){// create some characterschar char1 = 'A';char char2 = 'z';char char3 = 'R';char char4 = '@';char char5 = '0';// print the values and their typesConsole.WriteLine("value of char1: {0}, type of char1: {1}", char1, char1.GetType());Console.WriteLine("value of char2: {0}, type of char2: {1}", char2, char2.GetType());Console.WriteLine("value of char3: {0}, type of char3: {1}", char3, char3.GetType());Console.WriteLine("value of char4: {0}, type of char4: {1}", char4, char4.GetType());Console.WriteLine("value of char5: {0}, type of char5: {1}", char5, char5.GetType());//converting to Int32 & printing values & typesConsole.WriteLine("\nvalue: {0}, type: {1}", Convert.ToInt32(char1), Convert.ToInt32(char1).GetType());Console.WriteLine("value: {0}, type: {1}", Convert.ToInt32(char2), Convert.ToInt32(char2).GetType());Console.WriteLine("value: {0}, type: {1}", Convert.ToInt32(char3), Convert.ToInt32(char3).GetType());Console.WriteLine("value: {0}, type: {1}", Convert.ToInt32(char4), Convert.ToInt32(char4).GetType());Console.WriteLine("value: {0}, type: {1}", Convert.ToInt32(char5), Convert.ToInt32(char5).GetType());}}
GetType()
method.Int32
type using the Convert.ToInt32()
, and print the values and their types to the console.