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.ToUInt64 Method in C#
The Convert.ToUInt64() method in C# converts a specified value to a 64-bit unsigned integer (ulong). This method accepts various data types and returns their equivalent ulong representation.
Syntax
Following is the syntax for the Convert.ToUInt64() method −
public static ulong ToUInt64(object value) public static ulong ToUInt64(string value) public static ulong ToUInt64(char value) public static ulong ToUInt64(int value) public static ulong ToUInt64(double value)
Parameters
-
value − The value to convert. Can be of type
object,string,char, numeric types, or other convertible types.
Return Value
Returns a 64-bit unsigned integer (ulong) equivalent to the specified value. If the value is null, the method returns 0.
Using Convert.ToUInt64() with Different Data Types
Converting Character to UInt64
When converting a character, the method returns the ASCII value of that character −
using System;
public class Demo {
public static void Main() {
char ch = 'a';
ulong res = Convert.ToUInt64(ch);
Console.WriteLine("Converted char value '{0}' to {1}", ch, res);
char ch2 = 'Z';
ulong res2 = Convert.ToUInt64(ch2);
Console.WriteLine("Converted char value '{0}' to {1}", ch2, res2);
}
}
The output of the above code is −
Converted char value 'a' to 97 Converted char value 'Z' to 90
Converting String to UInt64
The method can parse numeric strings and convert them to ulong values −
using System;
public class Demo {
public static void Main() {
string str1 = "12345";
string str2 = "18446744073709551615"; // Maximum ulong value
ulong result1 = Convert.ToUInt64(str1);
ulong result2 = Convert.ToUInt64(str2);
Console.WriteLine("String '{0}' converted to: {1}", str1, result1);
Console.WriteLine("String '{0}' converted to: {1}", str2, result2);
}
}
The output of the above code is −
String '12345' converted to: 12345 String '18446744073709551615' converted to: 18446744073709551615
Converting Different Numeric Types
The method works with various numeric data types −
using System;
public class Demo {
public static void Main() {
int intValue = 100;
double doubleValue = 123.45;
decimal decimalValue = 999.99m;
ulong fromInt = Convert.ToUInt64(intValue);
ulong fromDouble = Convert.ToUInt64(doubleValue);
ulong fromDecimal = Convert.ToUInt64(decimalValue);
Console.WriteLine("int {0} to ulong: {1}", intValue, fromInt);
Console.WriteLine("double {0} to ulong: {1}", doubleValue, fromDouble);
Console.WriteLine("decimal {0} to ulong: {1}", decimalValue, fromDecimal);
}
}
The output of the above code is −
int 100 to ulong: 100 double 123.45 to ulong: 123 decimal 999.99 to ulong: 999
Key Rules
-
The method rounds floating-point values to the nearest integer.
-
Negative values will throw an
OverflowExceptionsinceulongis unsigned. -
Values exceeding the
ulongrange (0 to 18,446,744,073,709,551,615) will throw anOverflowException. -
If the input string cannot be parsed, a
FormatExceptionis thrown.
Conclusion
The Convert.ToUInt64() method provides a convenient way to convert various data types to 64-bit unsigned integers. It handles characters by returning their ASCII values, parses numeric strings, and converts other numeric types while applying appropriate rounding for floating-point values.
