Home » C#.Net

Convert hexadecimal string to an integer number in C#

C# | Convert hexadecimal string to an integer: Here, we are going to learn how to convert a given string that contains a hexadecimal number in an integer number?
Submitted by IncludeHelp, on February 09, 2019

Given a hexadecimal string and we have to convert it into an integer number.

Converting from hexadecimal string to integer

Let suppose you have a string "3039" which is a hexadecimal value of integer 12345, but this value is in string format, and you want to its integer (number) value.

To convert a hexadecimal string to an integer number – we use Convert.ToInt32() method.

Syntax:

    Convert.ToInt32(input_string, Input_base);

Here,

  • input_string is the input containing hexadecimal number in string format.
  • input_base is the base of the input value – for a hexadecimal value it will be 16.

Example:

    Input: "3039"
    Function call:
    Convert.ToInt32(input, 16);
    Output:
    12345

    Input: "303a"
    Function call:
    Convert.ToInt32(input, 16);
    Output:
    12346

    Input: "303ag" //not 'g' is not a valid hex digit
    Function call:
    Convert.ToInt32(input, 16);
    Output:
    Exception 

Code:

using System;
using System.Text;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            //hex number as string
            string input = "3039";
            int output = 0;
            //converting to integer 
            output = Convert.ToInt32(input, 16);
            //printing the value
            Console.WriteLine("Integer number: " + output);

            //hit ENTER to exit
            Console.ReadLine();
        }
    }
}

Output

    Integer number: 12345

Example with Exception handling

Code:

using System;
using System.Text;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            string input = "";
            int output = 0;
            try
            {
                //input string
                Console.Write("Enter a hexadecimal number: ");
                input = Console.ReadLine();

                //converting to integer
                output = Convert.ToInt32(input, 16);

                Console.WriteLine("Integer number: " + output);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

            //hit ENTER to exit
            Console.ReadLine();
        }
    }
}

Output

First run with valid input:
Enter a hexadecimal number: 3039
Integer number: 12345

Second run with valid input:
Enter a hexadecimal number: 303a
Integer number: 12346

Third run with invalid input:
Enter a hexadecimal number: 303ag
System.FormatException: Additional non-parsable characters are at the end of the
 string.
   at System.ParseNumbers.StringToInt(String s, Int32 radix, Int32 flags, Int32*
 currPos)
   at System.Convert.ToInt32(String value, Int32 fromBase)
   at ConsoleApplication3.Program.Main(String[] args) in F:\Ankur\SerialPort\Con
soleApplication3\ConsoleApplication3\Program.cs:line 19


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.