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
How to convert an integer to hexadecimal and vice versa in C#?
Converting integers to hexadecimal and vice versa is a common requirement in C# programming. C# provides several built-in methods to perform these conversions efficiently using ToString() for integer-to-hex conversion and Convert.ToInt32() or int.Parse() for hex-to-integer conversion.
Converting Integer to Hexadecimal
An integer can be converted to hexadecimal using the ToString() method with format specifiers −
Syntax
string hexValue = integerValue.ToString("X"); // Uppercase
string hexValue = integerValue.ToString("x"); // Lowercase
string hexValue = integerValue.ToString("X8"); // 8-digit padding
Example
using System;
public class Program {
public static void Main() {
int integerValue = 500;
Console.WriteLine($"Integer Value: {integerValue}");
string hexUppercase = integerValue.ToString("X");
string hexLowercase = integerValue.ToString("x");
string hexPadded = integerValue.ToString("X8");
Console.WriteLine($"Hexadecimal (Uppercase): {hexUppercase}");
Console.WriteLine($"Hexadecimal (Lowercase): {hexLowercase}");
Console.WriteLine($"Hexadecimal (8-digit): {hexPadded}");
}
}
The output of the above code is −
Integer Value: 500 Hexadecimal (Uppercase): 1F4 Hexadecimal (Lowercase): 1f4 Hexadecimal (8-digit): 000001F4
Converting Hexadecimal to Integer
A hexadecimal string can be converted to an integer using Convert.ToInt32() with base 16 or int.Parse() with NumberStyles.HexNumber −
Syntax
int value = Convert.ToInt32(hexString, 16); int value = int.Parse(hexString, System.Globalization.NumberStyles.HexNumber);
Using Convert.ToInt32()
using System;
public class Program {
public static void Main() {
string hexValue = "1F4";
Console.WriteLine($"Hexadecimal Value: {hexValue}");
int integerValue = Convert.ToInt32(hexValue, 16);
Console.WriteLine($"Integer Value: {integerValue}");
// Works with lowercase too
string hexLower = "ff";
int intLower = Convert.ToInt32(hexLower, 16);
Console.WriteLine($"Hex '{hexLower}' converts to: {intLower}");
}
}
The output of the above code is −
Hexadecimal Value: 1F4 Integer Value: 500 Hex 'ff' converts to: 255
Using int.Parse() with NumberStyles
using System;
using System.Globalization;
public class Program {
public static void Main() {
string hexValue = "ABCD";
Console.WriteLine($"Hexadecimal Value: {hexValue}");
int integerValue = int.Parse(hexValue, NumberStyles.HexNumber);
Console.WriteLine($"Integer Value: {integerValue}");
// Convert back to verify
string backToHex = integerValue.ToString("X");
Console.WriteLine($"Back to Hex: {backToHex}");
}
}
The output of the above code is −
Hexadecimal Value: ABCD Integer Value: 43981 Back to Hex: ABCD
Comparison of Methods
| Method | Usage | Advantage |
|---|---|---|
| Convert.ToInt32(hex, 16) | Hex to Integer | Simple, handles null values gracefully |
| int.Parse(hex, NumberStyles.HexNumber) | Hex to Integer | More explicit about number format |
| ToString("X") | Integer to Hex | Built-in formatting options |
Complete Conversion Example
using System;
public class HexConverter {
public static void Main() {
// Multiple integer values
int[] numbers = {10, 255, 4096, 65535};
Console.WriteLine("Integer to Hexadecimal Conversion:");
foreach (int num in numbers) {
string hex = num.ToString("X");
Console.WriteLine($"{num} -> 0x{hex}");
}
Console.WriteLine("\nHexadecimal to Integer Conversion:");
string[] hexValues = {"A", "FF", "1000", "FFFF"};
foreach (string hex in hexValues) {
int num = Convert.ToInt32(hex, 16);
Console.WriteLine($"0x{hex} -> {num}");
}
}
}
The output of the above code is −
Integer to Hexadecimal Conversion: 10 -> 0xA 255 -> 0xFF 4096 -> 0x1000 65535 -> 0xFFFF Hexadecimal to Integer Conversion: 0xA -> 10 0xFF -> 255 0x1000 -> 4096 0xFFFF -> 65535
Conclusion
Converting between integers and hexadecimal in C# is straightforward using ToString("X") for integer-to-hex and Convert.ToInt32(hex, 16) for hex-to-integer conversions. These methods handle both uppercase and lowercase hexadecimal formats and provide flexible formatting options for different use cases.
