# Convert Int to Hex in C#

This tutorial will discuss how to convert int to hex and a hex to int in C#.

## Convert Int to Hex With the `ToString()` Method in C

The Integer data type stores integer values of base 10 in C#. The `int` keyword declares a variable with the integer data type. The Hexadecimal data type has a base of 16. We can convert an integer data type to a hexadecimal string with the `ToString()` method in C#. We can pass the string format specifier `"X"` to the `ToString()` method to convert an integer to hexadecimal. See the following example.

``````using System;

namespace file_size
{
class Program
{
static void Main(string[] args)
{
int i = 99;
string hex = i.ToString("X");
Console.WriteLine(hex);
}
}
}
``````

Output:

``````63
``````

We initialized the integer variable `i` and converted it to the hexadecimal string `hex` with the `i.ToString("X")` method in C#. The variable `i` has an integer value of `99` which becomes `63` in hexadecimal.

## Convert Hex to Int With the `Convert.ToInt32()` Function in C

In the previous section, we discussed the method to convert from an integer value to a hexadecimal value. Now, we will convert the same hexadecimal value from the previous example back to an integer value in C#. The `Convert` class provides the functionality of conversion between various base data types in C#. The `Convert.ToInt32()` function converts any data type to the 32-bit integer data type in C#. We can pass the hexadecimal string along with the base `16` in the parameters of the `Convert.ToInt32()` function to convert the hexadecimal string to a 32-bit integer value. See the following example.

``````using System;

namespace file_size
{
class Program
{
static void Main(string[] args)
{
string hex = "63";
int i = Convert.ToInt32(hex, 16);
Console.WriteLine(i);
}
}
}
``````

Output:

``````99
``````

We converted the same hexadecimal string `hex` from the previous section to the integer variable `i` with the `Convert.ToInt32(hex, 16)` function in C#. The `hex` variable has a value of `63`, which becomes `99` in base 10.

Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

## Related Article - Csharp Integer

• Convert Int to Bool in C#
• Random Number in a Range in C#
• ## Related Article - Csharp Hex

• Random Number in a Range in C#
• Convert String to Int in C#