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
UInt32.ToString() Method in C# with Examples
The UInt32.ToString() method in C# is used to convert the numeric value of a UInt32 instance to its equivalent string representation. This method is particularly useful when you need to display unsigned 32-bit integer values as text or concatenate them with strings.
The UInt32 type represents unsigned 32-bit integers with values ranging from 0 to 4,294,967,295. The ToString() method provides several overloads to format the output according to different requirements.
Syntax
Following are the main syntax variations of the UInt32.ToString() method −
public override string ToString(); public string ToString(string format); public string ToString(IFormatProvider provider); public string ToString(string format, IFormatProvider provider);
Parameters
format − A standard or custom numeric format string that specifies how the value should be formatted.
provider − An object that supplies culture-specific formatting information.
Return Value
The method returns a string that represents the value of the current UInt32 instance.
Using Basic ToString() Method
The parameterless ToString() method converts the UInt32 value to its decimal string representation −
using System;
public class Demo {
public static void Main() {
uint val1 = 100;
uint val2 = 80;
Console.WriteLine("Value1 (String representation) = " + val1.ToString());
Console.WriteLine("Value2 (String representation) = " + val2.ToString());
bool res = val1.Equals(val2);
Console.WriteLine("Return value (comparison) = " + res);
if (res)
Console.WriteLine("val1 = val2");
else
Console.WriteLine("val1 != val2");
}
}
The output of the above code is −
Value1 (String representation) = 100 Value2 (String representation) = 80 Return value (comparison) = False val1 != val2
Using ToString() with Maximum Values
This example demonstrates ToString() with minimum and maximum UInt32 values −
using System;
public class Demo {
public static void Main() {
uint val1 = UInt32.MinValue;
uint val2 = UInt32.MaxValue;
Console.WriteLine("Minimum UInt32 value = " + val1.ToString());
Console.WriteLine("Maximum UInt32 value = " + val2.ToString());
uint midValue = val2 / 2;
Console.WriteLine("Mid-range value = " + midValue.ToString());
Console.WriteLine("UInt32 range: " + val1.ToString() + " to " + val2.ToString());
}
}
The output of the above code is −
Minimum UInt32 value = 0 Maximum UInt32 value = 4294967295 Mid-range value = 2147483647 UInt32 range: 0 to 4294967295
Using ToString() with Format Specifiers
The ToString(string format) overload allows you to format the output using standard numeric format strings −
using System;
public class Demo {
public static void Main() {
uint value = 255;
Console.WriteLine("Decimal: " + value.ToString("D"));
Console.WriteLine("Hexadecimal: " + value.ToString("X"));
Console.WriteLine("Hexadecimal (lowercase): " + value.ToString("x"));
Console.WriteLine("Fixed-point: " + value.ToString("F2"));
Console.WriteLine("Number with separators: " + value.ToString("N"));
uint largeValue = 1234567890;
Console.WriteLine("Large number formatted: " + largeValue.ToString("N0"));
}
}
The output of the above code is −
Decimal: 255 Hexadecimal: FF Hexadecimal (lowercase): ff Fixed-point: 255.00 Number with separators: 255.00 Large number formatted: 1,234,567,890
Common Use Cases
Display Values: Converting numeric values for user interface display
Logging: Converting values to strings for log files and debugging
String Concatenation: Combining numeric values with text
Data Export: Converting values to string format for file output or database operations
Conclusion
The UInt32.ToString() method provides a simple and flexible way to convert unsigned 32-bit integers to string representations. With its various overloads, you can format the output according to specific requirements, making it essential for display, logging, and data conversion operations in C# applications.
