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
Convert.ToUInt32 Method in C#
The Convert.ToUInt32 method in C# converts a specified value to a 32-bit unsigned integer (uint). This method can convert various data types including strings, integers, floating-point numbers, and other numeric types to an unsigned 32-bit integer.
The uint data type can hold values from 0 to 4,294,967,295, making it suitable for scenarios where you need positive integers only with a larger range than regular int.
Syntax
Following are the common overloads of the Convert.ToUInt32 method −
Convert.ToUInt32(string value) Convert.ToUInt32(int value) Convert.ToUInt32(double value) Convert.ToUInt32(object value)
Parameters
value − The value to be converted to a 32-bit unsigned integer. Can be a string representation of a number, numeric types, or objects that implement
IConvertible.
Return Value
Returns a uint (32-bit unsigned integer) equivalent of the specified value. If the value is null, it returns 0.
Converting String to UInt32
Example
using System;
public class Demo {
public static void Main() {
string str = "210";
uint res = Convert.ToUInt32(str);
Console.WriteLine("Converted string '{0}' to {1}", str, res);
Console.WriteLine("Data type: {0}", res.GetType());
}
}
The output of the above code is −
Converted string '210' to 210 Data type: System.UInt32
Converting Different Data Types
Example
using System;
public class Demo {
public static void Main() {
// Converting from different data types
int intValue = 150;
double doubleValue = 250.75;
bool boolValue = true;
uint fromInt = Convert.ToUInt32(intValue);
uint fromDouble = Convert.ToUInt32(doubleValue);
uint fromBool = Convert.ToUInt32(boolValue);
Console.WriteLine("From int {0}: {1}", intValue, fromInt);
Console.WriteLine("From double {0}: {1}", doubleValue, fromDouble);
Console.WriteLine("From bool {0}: {1}", boolValue, fromBool);
}
}
The output of the above code is −
From int 150: 150 From double 250.75: 251 From bool True: 1
Handling Exceptions
The Convert.ToUInt32 method can throw exceptions for invalid inputs −
Example
using System;
public class Demo {
public static void Main() {
try {
// Valid conversion
string validStr = "4294967295"; // Maximum uint value
uint result1 = Convert.ToUInt32(validStr);
Console.WriteLine("Valid conversion: {0}", result1);
// Null value returns 0
string nullStr = null;
uint result2 = Convert.ToUInt32(nullStr);
Console.WriteLine("Null conversion: {0}", result2);
} catch (Exception ex) {
Console.WriteLine("Error: {0}", ex.Message);
}
}
}
The output of the above code is −
Valid conversion: 4294967295 Null conversion: 0
Common Use Cases
User Input Validation − Converting string input from users to unsigned integers for calculations.
Data Processing − Converting numeric data from files or databases to
uintformat.Memory Addresses − Working with memory addresses or identifiers that are always positive.
Conclusion
The Convert.ToUInt32 method provides a reliable way to convert various data types to 32-bit unsigned integers in C#. It handles null values gracefully by returning zero and supports conversion from strings, numeric types, and boolean values with appropriate type casting and rounding where necessary.
