Mohanapriya R Mohanapriya R
Updated date Jul 18, 2024
In this article, we will discuss how to easily convert hexadecimal numbers to strings using C#.
  • 2.8k
  • 0
  • 0

What is Hexadecimal?

Hexadecimal, or hex, is a base-16 numeral system that uses 16 symbols to represent values. The symbols include 0-9 and A-F, where A stands for 10, B for 11, and so on. Hexadecimal is used in computing, especially in memory addresses, RGB color codes, and cryptographic applications.

Converting Hexadecimal to String in C#:

In C#, the Convert class provides a simple method to convert hexadecimal values to strings. The ToString method, combined with the Convert.ToInt32 method, allows us to achieve this conversion.

using System;

class Program
{
    static void Main()
    {
        // Hexadecimal value to be converted
        string hexValue = "57656c636f6d652c20546563686965486f6f6b21"; // Hex representation of "Welcome, TechieHook!"

        // Convert hex to byte array
        byte[] byteData = new byte[hexValue.Length / 2];
        for (int i = 0; i < byteData.Length; i++)
        {
            byteData[i] = Convert.ToByte(hexValue.Substring(i * 2, 2), 16);
        }

        // Convert byte array to string
        string stringValue = System.Text.Encoding.UTF8.GetString(byteData);

        // Output the result
        Console.WriteLine("Hexadecimal Value: " + hexValue);
        Console.WriteLine("Converted String: " + stringValue);
        Console.ReadKey();
    }
}

Output:

Hexadecimal Value: 57656c636f6d652c20546563686965486f6f6b21
Converted String: Welcome, TechieHook!

Comments (0)

There are no comments. Be the first to comment!!!