Mohanapriya R Mohanapriya R
Updated date Jun 05, 2024
In this article, we will learn how to convert binary decoded strings to readable text using a simple C# program.

Understanding Binary Decoded Strings:

Binary decoded strings represent characters in their binary form. Each character is typically represented by a set number of bits, such as 8 bits for ASCII characters. To convert these binary strings to normal text, we need to follow a systematic approach.

using System;
using System.Text;

class BinaryDecoder
{
    static void Main()
    {
        // Input: Binary decoded string
        string binaryString = "01001000 01100101 01101100 01101100 01101111";

        // Convert binary string to text
        string textOutput = BinaryToString(binaryString);

        // Output: Display the converted text
        Console.WriteLine("Converted Text: " + textOutput);
    }

    static string BinaryToString(string binary)
    {
        // Split the binary string into 8-bit chunks
        string[] binaryChunks = binary.Split(' ');

        // Convert each binary chunk to decimal and then to char
        StringBuilder result = new StringBuilder();
        foreach (string chunk in binaryChunks)
        {
            int decimalValue = Convert.ToInt32(chunk, 2);
            result.Append((char)decimalValue);
        }

        return result.ToString();
    }
}

We have defined a binary string representing the word "Hello". The BinaryToString method is used to convert the binary string to its corresponding text. Within the method, the binary string is split into 8-bit chunks, as each character is typically represented by 8 bits. Each binary chunk is then converted to its decimal equivalent using Convert.ToInt32. The decimal values are cast to characters using (char)decimalValue. The characters are concatenated to form the final text output.

Output:

Converted Text: Hello

Comments (0)

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