Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

character
int32
c#
communitycreator

How to convert a character to Int32 in C#

Theodore Kelechukwu Onyejiaku

Overview

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.

Syntax

Convert.ToInt32(character)
Syntax to convert a Character to Int32 in C#

Parameters

character: This is the character we want to convert to a 32-bit signed integer.

Return value

It returns a 32-bit signed integer that is the equivalent of the character.

Code example

Let's look at the code below:

using System;
class HelloWorld
{
    static void Main()
    {
      // create some characters
      char char1 = 'A'; 
      char char2 = 'z'; 
      char char3 = 'R';
      char char4 = '@';
      char char5 = '0';

      // print the values and their types
      Console.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 & types
      Console.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());
    }
}
Convert a Character to Int32 in C#

Code explanation:

  • Lines 7 to 11: We create some characters.
  • Lines 14 to 18: We print these characters and their class type using the GetType() method.
  • Lines 21 to 25: We convert the characters to Int32 type using the Convert.ToInt32(), and print the values and their types to the console.

RELATED TAGS

character
int32
c#
communitycreator

CONTRIBUTOR

Theodore Kelechukwu Onyejiaku
RELATED COURSES

View all Courses

Keep Exploring