c# - 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.
解决方案
您可以使用单行文本框并用逗号分隔“字节”,也可以使用多行文本框并每行有一个“字节”。任何你能想到的分隔符都可以,可能。
byte.Parse()
然后,您使用该方法将这些字节数转换为实际字节。代码可能类似于:
string[] splittedBytes = txtInputBytes.Text.Split(',');
byte[] bytes = splittedBytes.Select(byte.Parse).ToArray();
MessageBox.Show(Encoding.UTF8.GetString(bytes));
该Split()
方法将在每个分隔符处“中断”输入,在此示例中为逗号。下一行将输入的这些部分转换为实际字节,使用Select
Linq 方法将 应用于byte.Parse()
每个输入元素。您也可以使用for
语句或任何其他方式。然后将字节解码为实际字符串并显示它。
推荐阅读
- python - 散景 - 绘制的项目少于通过的项目数
- javascript - (Socket.io) - 为什么套接字对象在断开连接事件时会丢失自定义属性?
- pyqt5 - 在 QTableview PyQT 中显示图像
- flutter - 如何在颤动中使用 CachedNetworkImage 处理 404 异常
- python - 在 MDToolbar 的中心对齐标题
- html - 应禁用搜索框上的悬停效果
- vue.js - 从 Vue 中的 v-for 列表中删除重复项
- ios - Swift - 枚举内的枚举
- angular - Angular 子组件正在更新,但直到单击任意位置才能查看
- c++ - 如何从 ODBC 连接 SQLConnect?