首页 > 解决方案 > C# Bytes from Textbox to Value

问题描述

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.

标签: c#decode

解决方案


您可以使用单行文本框并用逗号分隔“字节”,也可以使用多行文本框并每行有一个“字节”。任何你能想到的分隔符都可以,可能。

byte.Parse()然后,您使用该方法将这些字节数转换为实际字节。代码可能类似于:

string[] splittedBytes = txtInputBytes.Text.Split(',');
byte[] bytes = splittedBytes.Select(byte.Parse).ToArray();
MessageBox.Show(Encoding.UTF8.GetString(bytes));

Split()方法将在每个分隔符处“中断”输入,在此示例中为逗号。下一行将输入的这些部分转换为实际字节,使用SelectLinq 方法将 应用于byte.Parse()每个输入元素。您也可以使用for语句或任何其他方式。然后将字节解码为实际字符串并显示它。


推荐阅读