Sai A Sai A
Updated date May 12, 2023
In this blog, we will learn to convert binary-encoded strings to normal strings in C# using two different methods - one using built-in conversion methods and the other using bitwise operations.

Introduction:

In today's digital age, data is often encoded in binary format. Binary encoding is the process of converting data into a binary code, which can be stored and transmitted more efficiently than traditional character-based encoding. However, binary-encoded strings are not easily readable by humans. In this blog, we will discuss how to convert binary-encoded strings to normal strings in C#.

Method 1: Using Convert.ToInt32() and Convert.ToChar() methods

The first method involves converting each binary-encoded character to its decimal equivalent and then converting it to a character using the Convert.ToChar() method.

The following program demonstrates this method:

using System;

class BinaryToStringConverter
{
    static void Main(string[] args)
    {
        string binaryString = "01101000 01100101 01101100 01101100 01101111";
        string[] binaryArray = binaryString.Split(' ');

        string result = "";

        foreach (string binary in binaryArray)
        {
            int decimalValue = Convert.ToInt32(binary, 2);
            char character = Convert.ToChar(decimalValue);
            result += character;
        }

        Console.WriteLine(result);
    }
}

Output:

hello

In this program, we first define the binary-encoded string "01101000 01100101 01101100 01101100 01101111" and split it into an array of individual binary values using the Split() method.

We then iterate over each binary value in the array and convert it to its decimal equivalent using the Convert.ToInt32() method. We specify the base of the input as 2 to indicate that we are converting from binary.

Next, we convert the decimal value to its corresponding character using the Convert.ToChar() method. Finally, we append each character to the result string.

Method 2: Using Bitwise Operations

The second method involves using bitwise operations to convert each binary-encoded character to its corresponding character.

The following program demonstrates this method:

using System;

class BinaryToStringConverter
{
    static void Main(string[] args)
    {
        string binaryString = "01101000 01100101 01101100 01101100 01101111";
        string[] binaryArray = binaryString.Split(' ');

        string result = "";

        foreach (string binary in binaryArray)
        {
            int decimalValue = 0;
            for (int i = 0; i < binary.Length; i++)
            {
                if (binary[binary.Length - i - 1] == '1')
                {
                    decimalValue += (int)Math.Pow(2, i);
                }
            }

            char character = (char)decimalValue;
            result += character;
        }

        Console.WriteLine(result);
    }
}

Output:

hello

In this program, we again define the binary-encoded string "01101000 01100101 01101100 01101100 01101111" and split it into an array of individual binary values using the Split() method.

We then iterate over each binary value in the array and convert it to its decimal equivalent using bitwise operations. We initialize a decimalValue variable to 0 and then iterate over each character in the binary string from right to left. If the character is '1', we add 2^i to decimalValue, where i is the index of the current character.

Next, we convert the decimal value to its corresponding character using a cast to char. Finally, we append each character to the result string.

Conclusion:

In this blog, we have discussed two methods for converting binary-encoded strings to normal strings in C#. The first method involves converting each binary-encoded character to its decimal equivalent and then converting it to a character using the Convert.ToInt32() and Convert.ToChar() methods. The second method involves using bitwise operations to convert each binary-encoded character to its corresponding character.

Comments (0)

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