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);
Call
What 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
Parameter
Description
value
Required. Text or number to convert.
fromBase
Optional. 2, 8, 10, or 16.
Return Value
Return Value
Description
int
32-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);
}
}
Output
42
50
How It Works
Digits in."42" is text until Convert runs.
Int out. Result type is 32-bit signed.
Add eight. Proves the value is numeric.
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));
}
}
Output
42
42
How It Works
Hex 2A. Base 16 equals decimal 42.
Binary twin."101010" base 2 is also 42.
fromBase choices. Only 2, 8, 10, and 16 are allowed.
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));
}
}
Output
0
3
-2
How It Works
Null → 0. Unlike int.Parse, Convert accepts null.
3.9 → 3. Truncates toward zero (not banker's round here).
−2.9 → −2. Toward-zero also applies for negatives.
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.