1

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.

1 Answer 1

2

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.

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

2 Comments

I hope it could be of some help :)
Also note that if you use input values outside the byte range [0-255] you get an error. And if you use a byte value greater than 127 and the bytes don't conform to UTF-8 you might get and error or gibberish output.

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.