This post will discuss how to convert an integer to hexadecimal in C# and vice versa.

Convert an Integer to a Hexadecimal in C#

1. Convert.ToString() method

The recommended approach is to use the built-in method Convert.ToString() for converting a signed integer value to its equivalent hexadecimal representation. This method is demonstrated below:

Download  Run Code

2. Int32.ToString() method

The Int32.ToString() method can be used to convert a numeric value to equivalent string representation using the specified format. The following example displays an Int32 value using standard numeric format specifiers for hexadecimal (i.e., X or x).

Download  Run Code

3. String.Format() method

The following code example demonstrates how to use the String.Format to convert an integer to equivalent hexadecimal representation.

Download  Run Code

Convert a Hexadecimal to an Integer in C#

We can use any of the following methods to convert the hexadecimal string to an integer in C#.

1. Int32.Parse() method

Download  Run Code

2. Convert.ToInt32() method

Download  Run Code

That’s all about conversion between Integer and Hexadecimal in C#.