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 Hex String to Hex Number in C#?
In C#, converting a hexadecimal string to its numeric value is a common operation. Hexadecimal strings represent numbers in base 16 using digits 0-9 and letters A-F. C# provides several methods to convert hex strings to different numeric types depending on your needs.
Syntax
Following is the syntax for converting hex strings using different methods −
Convert.ToInt32(hexString, 16) Convert.ToSByte(hexString, 16) int.Parse(hexString, NumberStyles.HexNumber)
Using Convert.ToInt32() Method
The most commonly used method is Convert.ToInt32() which converts a hex string to a 32-bit signed integer −
using System;
public class Program {
public static void Main() {
string str = "7D";
int result = Convert.ToInt32(str, 16);
Console.WriteLine("Hex string: " + str);
Console.WriteLine("Decimal value: " + result);
// Converting larger hex values
string largeHex = "FF";
Console.WriteLine("Hex " + largeHex + " = " + Convert.ToInt32(largeHex, 16));
}
}
The output of the above code is −
Hex string: 7D Decimal value: 125 Hex FF = 255
Using Convert.ToSByte() Method
For smaller hex values that fit in a signed byte (range: -128 to 127), use Convert.ToSByte() −
using System;
public class Program {
public static void Main() {
string str = "7D";
sbyte result = Convert.ToSByte(str, 16);
Console.WriteLine("Hex string: " + str);
Console.WriteLine("SByte value: " + result);
// Maximum value for SByte
string maxHex = "7F";
Console.WriteLine("Max SByte hex " + maxHex + " = " + Convert.ToSByte(maxHex, 16));
}
}
The output of the above code is −
Hex string: 7D SByte value: 125 Max SByte hex 7F = 127
Using int.Parse() with NumberStyles
Another approach is using int.Parse() with NumberStyles.HexNumber −
using System;
using System.Globalization;
public class Program {
public static void Main() {
string hexString = "A1B2";
int result = int.Parse(hexString, NumberStyles.HexNumber);
Console.WriteLine("Hex string: " + hexString);
Console.WriteLine("Decimal value: " + result);
// With 0x prefix (remove it first)
string prefixedHex = "0xDEAD";
string cleanHex = prefixedHex.Replace("0x", "");
Console.WriteLine("Original: " + prefixedHex);
Console.WriteLine("Converted: " + int.Parse(cleanHex, NumberStyles.HexNumber));
}
}
The output of the above code is −
Hex string: A1B2 Decimal value: 41394 Original: 0xDEAD Converted: 57005
Comparison of Methods
| Method | Return Type | Range | Best For |
|---|---|---|---|
| Convert.ToInt32() | int | -2,147,483,648 to 2,147,483,647 | General purpose hex conversion |
| Convert.ToSByte() | sbyte | -128 to 127 | Small hex values |
| int.Parse() with NumberStyles | int | -2,147,483,648 to 2,147,483,647 | When working with NumberStyles |
Conclusion
Converting hex strings to numeric values in C# can be accomplished using Convert.ToInt32(), Convert.ToSByte(), or int.Parse() with NumberStyles.HexNumber. Choose the appropriate method based on the expected range of your hex values and the desired output type.
