Hex To Int Conversion In C#

hex string to int banner

Introduction

Hex is a base 16 number system and C# has a built-in function that can convert a string hex value into an int value. In C#, hexadecimal is not a built-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 means that the numbering is represented 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

Corresponding int to hex conversion

#HexInt
111
222
333
444
555
666
777
888
999
10A10
11B11
12C12
13D13
14E14
15F15

C# also mimics this conversion table in the code. See the 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 hexadecimal as a string and converts it to a float. See the 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
Get Latest Updates