在文本框中显示一个字节数组 [英] Display the a byte array in text box

查看:140
本文介绍了在文本框中显示一个字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我在 c#中有代码从串口读取一个字节数组。

我必须在文本框中显示通过串口接收的这个字节数组数据。

这个字节数组需要编辑才能从发送回应用程序。

任何人都可以帮我找到解决方案。



如何在文本框中显示字节数组数据。

解决方案

< blockquote>问题尚未完全定义。字节可以表示文本,这是一个可能的选项,其他选项将字节显示为十六进制字符串的单独十进制。或其他。

第一个选项显示在解决方案1中,它也包含ASCII,但编码可能会有所不同,具体取决于某些要求。



第二个选项可能需要类似的内容:

  byte  [] example =  new   byte  { 1  2  3 ,}; 
System.StringBuilder stringBuilder = new System.StringBuilder();
foreach byte 示例)
stringBuilder.Append( byte .ToString()+ ); // 可以添加格式参数
yourTextBox = stringBuilder.ToString.Trim(); // 不是最有效的,只是为了简单





-SA


你可以这样做:



 byte [] example = new byte {0x01,0x02,0x03}; 
YourTextBox.Text = System.Text.Encoding.UTF8.GetString(示例)


Hi,

I have code in c# that read a byte array from a serial port.
I have to display this byte array data received via serial port, in a text box.
This byte array is needed to edit from the for send back to an application.
Anybody please help me to find a solution.

How to display byte array data in textbox.

解决方案

The problem is not fully defined. The bytes can represent text, this is one possible option, and other option would be showing the bytes as separate decimal of hexadecimal strings. Or something else.
First options is shown in Solution 1, which also covers ASCII, but the encoding can be different, depending on some requirements.

The second option may require something like;

byte[] example = new byte { 1, 2, 3, };
System.StringBuilder stringBuilder = new System.StringBuilder();
foreach(byte in example)
    stringBuilder.Append(byte.ToString() + ", "); // a format parameter can be added
yourTextBox = stringBuilder.ToString.Trim(); // not the most efficient, just for simplicity



—SA


You could do this:

byte[] example = new byte { 0x01, 0x02, 0x03 };
YourTextBox.Text = System.Text.Encoding.UTF8.GetString(example)


这篇关于在文本框中显示一个字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆