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.ToUInt16 Method in C#
The Convert.ToUInt16 method in C# converts a specified value to a 16-bit unsigned integer (ushort). This method can convert various data types including strings, integers, doubles, and other numeric types to a ushort value ranging from 0 to 65,535.
Syntax
Following are the common overloads of the Convert.ToUInt16 method −
public static ushort ToUInt16(string value); public static ushort ToUInt16(int value); public static ushort ToUInt16(double value); public static ushort ToUInt16(bool value);
Parameters
value − The value to convert to a 16-bit unsigned integer. Can be a string representation of a number, numeric type, or boolean value.
Return Value
Returns a 16-bit unsigned integer (ushort) equivalent to the specified value. The valid range is 0 to 65,535.
Converting String to UInt16
Example
using System;
public class Demo {
public static void Main() {
string str = "1307";
ushort res;
res = Convert.ToUInt16(str);
Console.WriteLine("Converted string '{0}' to {1}", str, res);
Console.WriteLine("Type: {0}", res.GetType());
}
}
The output of the above code is −
Converted string '1307' to 1307 Type: System.UInt16
Converting Different Data Types
Example
using System;
public class ConvertExample {
public static void Main() {
// Convert from int
int intValue = 42000;
ushort fromInt = Convert.ToUInt16(intValue);
Console.WriteLine("From int {0}: {1}", intValue, fromInt);
// Convert from double (truncates decimal part)
double doubleValue = 25000.75;
ushort fromDouble = Convert.ToUInt16(doubleValue);
Console.WriteLine("From double {0}: {1}", doubleValue, fromDouble);
// Convert from boolean
bool trueValue = true;
bool falseValue = false;
ushort fromTrue = Convert.ToUInt16(trueValue);
ushort fromFalse = Convert.ToUInt16(falseValue);
Console.WriteLine("From bool true: {0}", fromTrue);
Console.WriteLine("From bool false: {0}", fromFalse);
}
}
The output of the above code is −
From int 42000: 42000 From double 25000.75: 25000 From bool true: 1 From bool false: 0
Handling Overflow and Invalid Values
Example
using System;
public class OverflowExample {
public static void Main() {
try {
// Valid conversion
string validStr = "65535"; // Maximum ushort value
ushort maxValue = Convert.ToUInt16(validStr);
Console.WriteLine("Max value: {0}", maxValue);
// This will throw OverflowException
string overflowStr = "70000";
ushort overflow = Convert.ToUInt16(overflowStr);
}
catch (OverflowException ex) {
Console.WriteLine("OverflowException: {0}", ex.Message);
}
catch (FormatException ex) {
Console.WriteLine("FormatException: {0}", ex.Message);
}
}
}
The output of the above code is −
Max value: 65535 OverflowException: Value was either too large or too small for a UInt16.
Common Use Cases
| Input Type | Conversion Result | Notes |
|---|---|---|
| String "1234" | 1234 | Parses numeric string |
| Int 30000 | 30000 | Direct conversion if within range |
| Double 123.99 | 123 | Truncates decimal part |
| Boolean true | 1 | true becomes 1 |
| Boolean false | 0 | false becomes 0 |
Conclusion
The Convert.ToUInt16 method provides a reliable way to convert various data types to 16-bit unsigned integers. Always handle potential exceptions when converting values that might be out of range (0-65,535) or in invalid formats to ensure robust applications.
