Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# Convert.ToInt32 Method
The Convert.ToInt32 method in C# is used to convert various data types to a 32-bit signed integer. It is commonly used to convert strings, floating-point numbers, and other numeric types to integers, providing a versatile approach to type conversion.
Syntax
Following are the most common syntax forms for Convert.ToInt32 −
Convert.ToInt32(value); Convert.ToInt32(value, fromBase);
Parameters
- value − The value to convert (string, double, decimal, bool, etc.)
- fromBase − Optional. The base of the number system (2, 8, 10, or 16)
Return Value
Returns a 32-bit signed integer equivalent to the specified value. Throws FormatException if the string cannot be converted, or OverflowException if the value is outside the range of a 32-bit integer.
Converting String to Integer
The most common use case is converting string representations of numbers to integers −
using System;
class Program {
static void Main() {
string str = "12";
Console.WriteLine("String: " + str);
int n = Convert.ToInt32(str);
Console.WriteLine("Number: " + n);
Console.WriteLine("Type: " + n.GetType());
}
}
The output of the above code is −
String: 12 Number: 12 Type: System.Int32
Converting Different Data Types
The Convert.ToInt32 method can handle various data types including floating-point numbers, booleans, and characters −
using System;
class Program {
static void Main() {
// Converting double to int
double dbl = 15.7;
int fromDouble = Convert.ToInt32(dbl);
Console.WriteLine("From double 15.7: " + fromDouble);
// Converting boolean to int
bool flag = true;
int fromBool = Convert.ToInt32(flag);
Console.WriteLine("From boolean true: " + fromBool);
// Converting char to int
char ch = '9';
int fromChar = Convert.ToInt32(ch.ToString());
Console.WriteLine("From char '9': " + fromChar);
}
}
The output of the above code is −
From double 15.7: 16 From boolean true: 1 From char '9': 9
Using Different Number Bases
You can convert strings representing numbers in different bases (binary, octal, hexadecimal) to integers −
using System;
class Program {
static void Main() {
// Converting from binary (base 2)
string binary = "1010";
int fromBinary = Convert.ToInt32(binary, 2);
Console.WriteLine("Binary 1010 to decimal: " + fromBinary);
// Converting from hexadecimal (base 16)
string hex = "FF";
int fromHex = Convert.ToInt32(hex, 16);
Console.WriteLine("Hex FF to decimal: " + fromHex);
// Converting from octal (base 8)
string octal = "77";
int fromOctal = Convert.ToInt32(octal, 8);
Console.WriteLine("Octal 77 to decimal: " + fromOctal);
}
}
The output of the above code is −
Binary 1010 to decimal: 10 Hex FF to decimal: 255 Octal 77 to decimal: 63
Exception Handling
When converting strings that cannot be parsed as integers, Convert.ToInt32 throws exceptions. It's good practice to handle these exceptions −
using System;
class Program {
static void Main() {
string[] testValues = {"123", "abc", "999999999999999999", null};
foreach (string value in testValues) {
try {
int result = Convert.ToInt32(value);
Console.WriteLine($"'{value}' converted to: {result}");
} catch (FormatException) {
Console.WriteLine($"'{value}' - Invalid format");
} catch (OverflowException) {
Console.WriteLine($"'{value}' - Value too large");
} catch (ArgumentNullException) {
Console.WriteLine("null - Cannot convert null value");
}
}
}
}
The output of the above code is −
'123' converted to: 123 'abc' - Invalid format '999999999999999999' - Value too large null - Cannot convert null value
Conclusion
The Convert.ToInt32 method provides a robust way to convert various data types to integers in C#. It supports multiple number bases and automatically handles rounding for floating-point numbers, but developers should implement proper exception handling for production code to manage invalid input gracefully.
