Print integer values from an array of strings containing hexadecimal values in C#

Print integer values from a variety of strings containing hexadecimal values in C#

C# printing integer values from a variety of hexadecimal strings: Here, we will figure out how to change over a variety of strings that contains hexadecimal values in integers?

Changing over an exhibit of hexadecimal strings to integers

Let guess you have a portion of the strings (for example exhibit of strings) containing hexadecimal values like “AA”, “ABCD”, “ff21”, “3039”, “FAFA” which are proportionate to integers 170, 43981, 65313, 12345, 64250.

As we have written in the past post: convert a hexadecimal string to an integer, we use Convert.ToInt32() capacity to change over the values.

We will get to everything utilizing a foreach circle, and convert the thing to an integer utilizing base value 16.

Code:

using System;
using System.Text;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] str = { "AA", "ABCD", "ff21", "3039", "FAFA"};
            int num = 0;
            try
            {
                //using foreach loop to access each items
                //and converting to integer numbers 
                foreach (string item in str)
                {
                    num = Convert.ToInt32(item, 16);
                    Console.WriteLine(num);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

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

Output:

170
43981
65313
12345
64250

Leave a Comment