C# Convert ToInt32() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Convert

Definition

In C#, Convert.ToInt32 returns an int (32-bit signed integer) from text, numbers, or objects.

It is the Convert method beginners use most. Prefer int.TryParse when the string might be invalid.

This is a static member of System.Convert. You call it with the type name, such as Convert.ToInt32(...). Convert is the everyday API for converting between common CLR types in C#.

Syntax

Pass digit text, a number, or a base for binary/hex strings.

C#
int a = Convert.ToInt32("42");
int b = Convert.ToInt32(3.9);   // truncates toward 0 → 3
int c = Convert.ToInt32("2A", 16);
CallWhat it does
Convert.ToInt32("42")Decimal string → int.
Convert.ToInt32("2A", 16)Hex string with base 16.
Convert.ToInt32(3.9)Double truncated toward zero → 3.

null → 0. Bad text → FormatException. Out of int range → OverflowException. Fractional doubles truncate toward zero.

Library declarations
C#
public static int ToInt32(string? value);
public static int ToInt32(string? value, int fromBase);
public static int ToInt32(double value);

Parameters

ParameterDescription
valueRequired. Text or number to convert.
fromBaseOptional. 2, 8, 10, or 16.

Return Value

Return ValueDescription
int32-bit signed result.

C# Examples

Example 1 — String Digits to Int

The classic Convert.ToInt32 beginner call.

C#
using System;

class Program {
    static void Main() {
        int n = Convert.ToInt32("42");
        Console.WriteLine(n);
        Console.WriteLine(n + 8);
    }
}

How It Works

  1. Digits in. "42" is text until Convert runs.
  2. Int out. Result type is 32-bit signed.
  3. Add eight. Proves the value is numeric.
  4. Everyday API. Most tutorials start here.

Example 2 — Hex String Overload

Use fromBase to parse hex digits.

C#
using System;

class Program {
    static void Main() {
        int n = Convert.ToInt32("2A", 16);
        Console.WriteLine(n);
        Console.WriteLine(Convert.ToInt32("101010", 2));
    }
}

How It Works

  1. Hex 2A. Base 16 equals decimal 42.
  2. Binary twin. "101010" base 2 is also 42.
  3. fromBase choices. Only 2, 8, 10, and 16 are allowed.
  4. Same result. Two notations, one int value.

Example 3 — Null Zero and Truncation

Show null → 0 and double truncation toward zero.

C#
using System;

class Program {
    static void Main() {
        Console.WriteLine(Convert.ToInt32(null));
        Console.WriteLine(Convert.ToInt32(3.9));
        Console.WriteLine(Convert.ToInt32(-2.9));
    }
}

How It Works

  1. Null → 0. Unlike int.Parse, Convert accepts null.
  2. 3.9 → 3. Truncates toward zero (not banker's round here).
  3. −2.9 → −2. Toward-zero also applies for negatives.
  4. Know the rule. Do not expect Math.Round behavior.

Frequently Asked Questions

Both parse digit text. Convert returns 0 for null; Parse throws. Prefer TryParse for soft failure.
It truncates toward zero (3.9 → 3).
Yes: Convert.ToInt32("FF", 16).
Values outside Int32 range throw OverflowException.
FormatException is thrown.

Next: ToInt64()

Convert a value to long (Int64) for larger whole numbers with System.Convert.

ToInt64() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful