Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display byte array in a text box without converting to readable string [duplicate]

Tags:

c#

I am trying to take ASCII from textBox1 and display the text in binary in textBox2. A simple ASCII to Binary converter.

        private void button1_Click(object sender, EventArgs e)
    {
        byte[] inVAR = System.Text.Encoding.ASCII.GetBytes(textBox1.Text);
        string outVAR = inVAR.ToString();
        textBox2.Text = outVAR;

    }

This of course results in the output being the same as the input, since I am converting the byte array back in to a readable string.

My question is how can I get the ASCII text to convert to binary but also a string type so that I can display it in the text box.

Essentially I am asking how do I create this ASCII to Binary converter because my method seems wrong.

Thanks!

Resolved! Thank you SLaks and OlimilOops:

textBox2.Text = string.Join(" ", inVAR.Select(b => Convert.ToString(b, 2).ToUpper()));
like image 655
Taurophylax Avatar asked Oct 03 '22 02:10

Taurophylax


1 Answers

It sounds like you want to display the numeric value of each byte, presumably separated by some kind of character:

string.Join("separator", bytes)

If you want to display in a base, you can use LINQ:

bytes.Select(b => Convert.ToString(b, 2))
like image 183
SLaks Avatar answered Oct 13 '22 10:10

SLaks