How to convert a C# String into a Hexadecimal value

In this article we will discuss about how to get the hexadecimal values of the corresponding character in a C# String.
  • 13561

In this article we are discussing that how to get the Corresponding hexadecimal value for each character in a string. To do this first we will convert the string into character array by using the ToCharArray() method of C# and then we will store in the character of array.

From where we will fetch one by one characters through foreach loop and then will get the integral value of the character. After in order to convert the decimal value to hexadecimal value in string form we will use the format method of the String class.

Lets a have a look on the following example:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace ConsoleApplication15

{

    class Program

    {

        static void Main(string[] args)

        {

            string str = "Good Morning!";

            char[] counting = str.ToCharArray();

            foreach (char words in counting)

            {

                int value = Convert.ToInt32(words);

                string hexaOutput = String.Format("{0:X}", value);

                Console.WriteLine("Hexadecimal value of {0} is {1}", words, hexaOutput);

            }

            Console.ReadLine();

        }

    }

}

Output:

 Image22.jpg

Further Readings
 
You may also want to read these related articles.
Ask Your Question 
 
Got a programming related question? You may want to post your question here
 

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.