C# Convert
last modified July 5, 2023
In this article we show to convert types in C# with Convert
class.
Convert
transforms a base data type to another base data type.
The Convert class is located in the System namespace.
Type conversion or typecasting refers to changing an entity of one data type into another. There are two types of conversion: implicit and explicit. Implicit type conversion, also known as coercion, is an automatic type conversion by the compiler.
C# convertion types
In the next example, we present three ways of data convertion.
int n = 12; string s = "15"; string res = n + s; Console.WriteLine(res); int res2 = int.Parse(s) + n; Console.WriteLine(res2); int res3 = Convert.ToInt32(s) + n; Console.WriteLine(res3);
The program defines an integer and a string. We add those two values.
int res = n + s;
By default, the compiler implicitly converts the integer to a string and adds two strings.
int res2 = int.Parse(s) + n;
We can use the int.Parse
method to explicitly convert the string
to an integer and add two integers.
int res3 = Convert.ToInt32(s) + n;
Alternatively, we can use the Convert.ToInt32
method. The
Convert
class contains several methods for common type convertions.
$ dotnet run 1215 27 27
C# Conver.ToBoolean
The Conver.ToBoolean
method converts a specified value to an
equivalent Boolean value.
double n1 = 0.3; int n2 = 3; int n3 = 0; int n4 = -1; decimal d = 0.35m; Console.WriteLine(Convert.ToBoolean(n1)); Console.WriteLine(Convert.ToBoolean(n2)); Console.WriteLine(Convert.ToBoolean(n3)); Console.WriteLine(Convert.ToBoolean(n4)); Console.WriteLine(Convert.ToBoolean(d));
In the program, we convert double, int, and decimal values to booleans.
$ dotnet run True True False True True
C# Convert hexadecimal string
The Convert.ToHexString
converts an array of 8-bit unsigned
integers to its equivalent string representation that is encoded with uppercase
hex characters.
The Convert.FromHexString
converts the specified string, which
encodes binary data as hex characters, to an equivalent 8-bit unsigned integer
array.
using System.Text; string msg = "an old falcon"; byte[] data = Encoding.ASCII.GetBytes(msg) ; string hexstr = Convert.ToHexString(data); Console.WriteLine(hexstr); byte[] data2 = Convert.FromHexString(hexstr); Console.WriteLine(Encoding.ASCII.GetString(data2));
The program converts a message to a hexadecimal string and vice versa.
$ dotnet run 616E206F6C642066616C636F6E an old falcon
C# Convert.ToBase64String
The Convert.ToBase64String
converts an array of 8-bit unsigned
integers to its equivalent string representation that is encoded with base-64
digits.
Conversely, the Convert.FromBase64String
method converts Converts
the specified string, which encodes binary data as base-64 digits, to an
equivalent 8-bit unsigned integer array.
Base64 encoding is widely used for sending e-mail attachments.
using System.Text; string msg = "an old falcon"; byte[] data = Encoding.ASCII.GetBytes(msg); Console.WriteLine(BitConverter.ToString(data)); string base64 = Convert.ToBase64String(data); Console.WriteLine(base64); Console.WriteLine("---------------------"); byte[] data2 = Convert.FromBase64String(base64); Console.WriteLine(BitConverter.ToString(data)); Console.WriteLine(Encoding.ASCII.GetString(data));
In the program, we convert a string to a byte array and later the array into the Base64 string. They we reverse the process.
$ dotnet run 61-6E-20-6F-6C-64-20-66-61-6C-63-6F-6E YW4gb2xkIGZhbGNvbg== --------------------- 61-6E-20-6F-6C-64-20-66-61-6C-63-6F-6E an old falcon
C# Convert.ToDateTime
The Convert.ToDateTime
converts the specified string representation
of a date and time to an equivalent date and time value.
public static DateTime ToDateTime (string? value);
This is the syntax of the method.
DateTime now = DateTime.Now; Console.WriteLine(now); string d = now.ToString(); Console.WriteLine(d); DateTime dt = Convert.ToDateTime(d); Console.WriteLine(dt);
In the example, we convert the current datetime to a string and the string back to a datetime.
$ dotnet run 10/23/2022 6:58:09 PM 10/23/2022 6:58:09 PM 10/23/2022 6:58:09 PM
Source
Convert class - language reference
In this article we have shown how to convert types in C# with Convert
.
Author
List all C# tutorials.