Reputation: 23
I have a Winform with one input textbox, one button and one output textbox.
Right now my code works the following way:
I click the button and the preset bytes in "string value" will be decoded to readable text and output to the output textbox using "string decoding1".
private void button1_Click(object sender, EventArgs e)
{
string value = decoding1(new byte[]
{
104,
107,
102,
102,
110,
103,
116
});
}
public string decoding1(byte[] byte_0)
{
textBox2.Text = Encoding.UTF8.GetString(decoding2(byte_0));
return Encoding.UTF8.GetString(decoding2(byte_0));
}
But now I want to be able to input these bytes "104, 107,..." into the input textbox so the program decodes and outputs them, otherwise I would have to manually input different bytes into the source code each time and this would be a waste of time for me.
How could I approach that, thanks a lot for your help.
Upvotes: 1
Views: 299
Reputation: 974
You can use a single line TextBox and separate your "bytes" with comma or use a multi-line TextBox and have one "byte" per line. Any separator you can think is OK, probably.
Then, you convert those byte number to actual bytes using the byte.Parse()
method. The code could go something like:
string[] splittedBytes = txtInputBytes.Text.Split(',');
byte[] bytes = splittedBytes.Select(byte.Parse).ToArray();
MessageBox.Show(Encoding.UTF8.GetString(bytes));
The Split()
method will "break" the input at each separator, a comma in this example. The next line converts those parts of input into actual bytes, using the Select
Linq method to apply the byte.Parse()
to each input element. You can also use a for
statement or any other way. Then decode the bytes to a actual string and display it.
Upvotes: 2