4

I want to convert text into binary and after that, to try convert binary into string text. How can i convert tobin back into text if it is already a string?

  private void iTalk_Button_12_Click(object sender, EventArgs e)
    {

        ambiance_RichTextBox2.Text = tobin(ambiance_RichTextBox1.Text);

    }
    public string tobin(string inp)
    {
        StringBuilder sb = new StringBuilder();
        foreach (char L in inp.ToCharArray())
        {
            sb.Append(Convert.ToString(L, 2).PadLeft(8, '0'));
        }
        return sb.ToString();
    }
2
  • Oh, where is the part when You're cnverting Your string to binary ? Commented Mar 30, 2015 at 12:05
  • you can refer this fluxbytes.com/csharp/… link. Commented Mar 30, 2015 at 12:06

4 Answers 4

4
        private void iTalk_Button_12_Click(object sender, EventArgs e)
        {
            ambiance_RichTextBox2.Text = BinaryToString(ambiance_RichTextBox1.Text);
           //use what u need: BinaryToString or StringToBinary.
        }

Convert String to Binary:

        public static string StringToBinary(string data)
        {
            StringBuilder sb = new StringBuilder();

            foreach (char c in data.ToCharArray())
            {
                sb.Append(Convert.ToString(c, 2).PadLeft(8, '0'));
            }
            return sb.ToString();
        }

Convert Binary to String:

        public static string BinaryToString(string data)
        {
            List<Byte> byteList = new List<Byte>();

            for (int i = 0; i < data.Length; i += 8)
            {
                byteList.Add(Convert.ToByte(data.Substring(i, 8), 2));
            }

            return Encoding.ASCII.GetString(byteList.ToArray());
        }

Good luck!

Sign up to request clarification or add additional context in comments.

Comments

1

You can use like this,

public static string BinaryToString(string data)
{
List<Byte> byteList = new List<Byte>();

for (int i = 0; i < data.Length; i += 8)
{
 byteList.Add(Convert.ToByte(data.Substring(i, 8), 2));
}

return Encoding.ASCII.GetString(byteList.ToArray());
}

Comments

1

Currently you are converting a char (which can be represented as a number) to its binary representation (this number is the ASCII number). But if you want to convert a string to binary, you should use an encoding. The encoding determines how the text is converted to binary.

For example:

static void Main(string[] args)
{
    string input = "This is an example text.";

    Console.WriteLine(input);
    string asBin = ToBinary(input);
    Console.WriteLine(asBin);
    string asText = ToText(asBin);
    Console.WriteLine(asText);
}

static string ToBinary(string input, System.Text.Encoding encoding = null)
{
    if (encoding == null)
        encoding = System.Text.Encoding.UTF8;

    var builder = new System.Text.StringBuilder();
    var bytes = encoding.GetBytes(input); // Convert the text to bytes using the encoding

    foreach (var b in bytes)
        builder.Append(Convert.ToString(b, 2).PadLeft(8, '0')); //Convert the byte to its binary representation

    return builder.ToString();
}

static string ToText(string bytes, System.Text.Encoding encoding = null)
{
    if (encoding == null)
        encoding = System.Text.Encoding.UTF8;

    var byteCount = 8;
    var byteArray = new byte[bytes.Length / 8]; // An array for the bytes
    for (int i = 0; i < bytes.Length / byteCount; i++)
    {
        var subBytes = bytes.Substring(i * byteCount, byteCount); // Get a subpart of 8 bits
        var b = Convert.ToByte(subBytes.TrimStart('0'), 2); // Convert the subpart to a byte
        byteArray[i] = b; // Add the byte to the array
    }

    return encoding.GetString(byteArray); // Convert the array to text using the right encoding.
}

Now if you want to use ASCII encoding, you can call the functions as follows:

Console.WriteLine(input);
string asBin = ToBinary(input, System.Text.Encoding.ASCII);
Console.WriteLine(asBin);
string asText = ToText(asBin, System.Text.Encoding.ASCII);
Console.WriteLine(asText);

Comments

1

To convert a string to binary

string s = "hai"; 

byte []arr = System.Text.Encoding.ASCII.GetBytes(s);

To convert binary to string

byte[] arr ;

string s =  Encoding.ASCII.GetString(arr);

2 Comments

How can i display byte []arr in my richTextBox? This- ambiance_RichTextBox2.Text = arr.ToString(); gives me just System.Byte[] message.
string s = "hai"; byte []arr = System.Text.Encoding.ASCII.GetBytes(s);richTextBox1.Text = Encoding.UTF8.GetString(arr);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.