- Code Snippets
- Hex String To Int
Hex String To Int

Introduction
Hex is a base 16 number system and C# has a built in function that can convert a string hex value into a int value. Hexadecimal is not a bult in type so you'll see the hexadecimal in the form of a string.
What is a hexadecimal
It is a numbering system that goes from digits 0-9 and then to A-F with a single digit. That is means that the numbering is reprented by 16 symbols. It can be converted into binary, int forms but this will only look at a hex to int form.
Code Snippet
static void Main(string[] args)
{
Console.WriteLine($"HexValue:9C, IntegerValue:{StringHexToInt("9C")}");
Console.WriteLine($"HexValue:F3, IntegerValue:{StringHexToInt("F3")}");
Console.WriteLine($"HexValue:3B, IntegerValue:{StringHexToInt("3B")}");
Console.WriteLine($"HexValue:33D, IntegerValue:{StringHexToInt("33D")}");
Console.WriteLine($"HexValue:2E, IntegerValue:{StringHexToInt("2E")}");
}
private static int StringHexToInt(string hexValue)
{
int integerValue = 0;
if (int.TryParse(hexValue, System.Globalization.NumberStyles.HexNumber, CultureInfo.CurrentCulture, out integerValue))
{
return integerValue;
}
return 0;
}
Output
HexValue:9C, IntegerValue:156
HexValue:F3, IntegerValue:243
HexValue:3B, IntegerValue:59
HexValue:33D, IntegerValue:829
HexValue:2E, IntegerValue:46
Coressponding int to hex conversion
# | Hex | Int |
---|---|---|
1 | 1 | 1 |
2 | 2 | 2 |
3 | 3 | 3 |
4 | 4 | 4 |
5 | 5 | 5 |
6 | 6 | 6 |
7 | 7 | 7 |
8 | 8 | 8 |
9 | 9 | 9 |
10 | A | 10 |
11 | B | 11 |
12 | C | 12 |
13 | D | 13 |
14 | E | 14 |
15 | F | 15 |
C# also mimics this conversion table in the code. See code snippet below.
Console.WriteLine($"HexValue:0, IntegerValue:{StringHexToInt("0")}");
Console.WriteLine($"HexValue:1, IntegerValue:{StringHexToInt("1")}");
Console.WriteLine($"HexValue:2, IntegerValue:{StringHexToInt("2")}");
Console.WriteLine($"HexValue:3, IntegerValue:{StringHexToInt("3")}");
Console.WriteLine($"HexValue:4, IntegerValue:{StringHexToInt("4")}");
Console.WriteLine($"HexValue:5, IntegerValue:{StringHexToInt("5")}");
Console.WriteLine($"HexValue:6, IntegerValue:{StringHexToInt("6")}");
Console.WriteLine($"HexValue:7, IntegerValue:{StringHexToInt("7")}");
Console.WriteLine($"HexValue:8, IntegerValue:{StringHexToInt("8")}");
Console.WriteLine($"HexValue:9, IntegerValue:{StringHexToInt("9")}");
Console.WriteLine($"HexValue:A, IntegerValue:{StringHexToInt("A")}");
Console.WriteLine($"HexValue:B, IntegerValue:{StringHexToInt("B")}");
Console.WriteLine($"HexValue:C, IntegerValue:{StringHexToInt("C")}");
Console.WriteLine($"HexValue:D, IntegerValue:{StringHexToInt("D")}");
Console.WriteLine($"HexValue:E, IntegerValue:{StringHexToInt("E")}");
Console.WriteLine($"HexValue:F, IntegerValue:{StringHexToInt("F")}");
Console Output:
HexValue:0, IntegerValue:0
HexValue:1, IntegerValue:1
HexValue:2, IntegerValue:2
HexValue:3, IntegerValue:3
HexValue:4, IntegerValue:4
HexValue:5, IntegerValue:5
HexValue:6, IntegerValue:6
HexValue:7, IntegerValue:7
HexValue:8, IntegerValue:8
HexValue:9, IntegerValue:9
HexValue:A, IntegerValue:10
HexValue:B, IntegerValue:11
HexValue:C, IntegerValue:12
HexValue:D, IntegerValue:13
HexValue:E, IntegerValue:14
HexValue:F, IntegerValue:15
Hex To Float
Another hex conversion we can do is hex to float. This method takes in a hexdecstring and converts it to an float. See code sample below.
using System.Globalization;
Console.WriteLine($"HexValue:9C, IntegerValue:{StringHexToFloat("9C")}");//Output value to the console
Console.WriteLine($"HexValue:F3, IntegerValue:{StringHexToFloat("F3")}");//Output value to the console
Console.WriteLine($"HexValue:3B, IntegerValue:{StringHexToFloat("3B")}");//Output value to the console
Console.WriteLine($"HexValue:33D, IntegerValue:{StringHexToFloat("33D")}");//Output value to the console
Console.WriteLine($"HexValue:2E, IntegerValue:{StringHexToFloat("2E")}");//Output value to the console
float StringHexToFloat(string hexValue)
{
if (uint.TryParse(hexValue, System.Globalization.NumberStyles.AllowHexSpecifier, CultureInfo.CurrentCulture, out uint number))//convert hex into a number
{
byte[] byteArr = BitConverter.GetBytes(number);//Convert number into a byte array
float floatValue = BitConverter.ToSingle(byteArr, 0);//Use byteArray and convert it into a float
return floatValue;
}
return 0;
}
Console Output:
HexValue:9C, IntegerValue:2.19E-43
HexValue:F3, IntegerValue:3.4E-43
HexValue:3B, IntegerValue:8.3E-44
HexValue:33D, IntegerValue:1.162E-42
HexValue:2E, IntegerValue:6.4E-44