Convert String to Hex in C#

  1. Convert String to Hex With the BitConverter.ToString() Method in C
  2. Convert String to Hex With the String.Format() Method in C

This tutorial will discuss the methods to a string to hex in C#.

Convert String to Hex With the BitConverter.ToString() Method in C

If we have a string that contains numerical values of base 10 and need to convert it to a string that contains numerical values of base 16, we can use the BitConverter.ToString() method for that. The BitConverter.ToString(x) method in C# converts each element in the array of bytes x to a hexadecimal value. To use the BitConverter.ToString() method, we have to convert our string variable to an array of bytes with the Encoding.Default.GetBytes() method. This method converts a string variable to an array of bytes in C#. The BitConverter.ToString() method returns a hexadecimal string in which each value is separated with -. We can use the String.Replace() method to remove the - separator.

using System;
using System.Linq;
using System.Text;

namespace string_to_hex
{
    class Program
    {
        static void Main(string[] args)
        {
            string decString = "0123456789";
            byte[] bytes = Encoding.Default.GetBytes(decString);
            string hexString = BitConverter.ToString(bytes);
            hexString = hexString.Replace("-", "");
            Console.WriteLine(hexString);
        }
    }
}

Output:

30313233343536373839

In the above code, we converted the string decString with decimal values to the string hexString with hexadecimal values with the BitConverter.ToString() method in C#.

Convert String to Hex With the String.Format() Method in C

The String.Format() method formats a string according to the given format specifier in C#. The {0:X2} format specifier specifies the hexadecimal format. We can use the {0:X2} format specifier inside the String.Format() method to format a string with decimal values to a string with hexadecimal values. We can use the LINQ to easily format each character of the decimal string to a hexadecimal format.

using System;
using System.Linq;
using System.Text;

namespace string_to_hex
{
    class Program
    {
        static void Main(string[] args)
        {
            string decString = "0123456789";
            var hexString = string.Join("",
                decString.Select(c => String.Format("{0:X2}", Convert.ToInt32(c))));
            Console.WriteLine(hexString);
        }
    }
}

Output:

30313233343536373839

In the above code, we converted the string variable decString with decimal values to the string variable hexString with hexadecimal values with the String.Format() method and LINQ in C#.

Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Csharp String

  • C# Convert Byte Array to String
  • Generate Random Alphanumeric Strings in C#
  • Related Article - Csharp Hex

  • Enum Strings in C#
  • Compare Two Strings by Ignoring Case in C#