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()));
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With