(C#) 如何使 HexDump 格式与 TextBox 一起使用?
(C#) How to make HexDump formating works with TextBox?
我在 Windows 表单上有 TextBox
,我从 com 端口成功接收了十六进制数据,但它显示在一行中,但我需要它以 HexDumpFormat 格式显示。
我现在得到的:
搜索后找到示例代码:Quick and Dirty Hex dump。这好像是我需要的,TC说我们只需要把他的函数粘贴到我的代码中,然后在我们需要的地方调用这个函数,但我很困惑如何让它与我的代码一起工作?非常感谢。 (我无法附加超过 3 links,因此您可以在 link 页面上看到 HexDump 格式的样子。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
namespace WindowsFormsApplication8
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
string rs;
byte re;
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e) // Da
{
try
{
//rs = serialPort1.ReadByte();
//re = Convert.ToByte(serialPort1.ReadByte());
rs = serialPort1.ReadExisting();
System.Text.Encoding.ASCII.GetString(new[] { re });
this.Invoke(new EventHandler(type));
}
catch (System.TimeoutException) { }
}
void type(object s, EventArgs e) // receive data
{
textBox4.Text += rs;
}
}
}
我尝试了 link 中的代码。这个简单的例子:
byte[] byte_array = new byte[5];
byte_array[0] = 0xAC;
byte_array[1] = 0x12;
byte_array[2] = 0xA0;
byte_array[3] = 0xC5;
byte_array[4] = 0x88;
Console.WriteLine(Utils.HexDump(byte_array, byte_array.Length));
产生以下输出:
重要的是您指定应在 1 行中打印数组中的多少字节。
您需要将 SerialPort
中的数据转换为字节数组。我建议尝试以下代码:
SerialPort serialPort1 = new SerialPort();
byte[] byte_array = new byte[1024];
int counter = 0;
while (serialPort1.BytesToRead > 0)
{
byte_array[counter] = (byte)serialPort1.ReadByte();
counter++;
}
编辑
为了能够使用来自 Link 的代码,向项目添加一个新的 class 文件。并将代码复制粘贴到其中。确保 *.cs class 文件与您的 Form1
class.
在同一个项目中
编辑 2:
如果您收到字符串形式的十六进制数字。您可以自己将其转换为字节数组,然后使用 link 中的 Utils
class 来显示它。这是完成此操作的一个小示例代码:
string rs = "0004"; // example from your code
byte[] byte_array = new byte[rs.Length/2]; // array is half as long as your string
int idx = 0; // count variable to index the string
int cnt = 0; // count variable to index the byte array
while (idx < rs.Length-1)
{
byte_array[cnt] = (byte)Convert.ToInt32(rs.Substring(idx, 2), 16);
idx += 2;
cnt++;
}
然后你可以这样打印结果:
textBox4.Text += Utils.HexDump(byte_array, cnt) + Environment.NewLine;
我在 Windows 表单上有 TextBox
,我从 com 端口成功接收了十六进制数据,但它显示在一行中,但我需要它以 HexDumpFormat 格式显示。
我现在得到的:
搜索后找到示例代码:Quick and Dirty Hex dump。这好像是我需要的,TC说我们只需要把他的函数粘贴到我的代码中,然后在我们需要的地方调用这个函数,但我很困惑如何让它与我的代码一起工作?非常感谢。 (我无法附加超过 3 links,因此您可以在 link 页面上看到 HexDump 格式的样子。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
namespace WindowsFormsApplication8
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
string rs;
byte re;
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e) // Da
{
try
{
//rs = serialPort1.ReadByte();
//re = Convert.ToByte(serialPort1.ReadByte());
rs = serialPort1.ReadExisting();
System.Text.Encoding.ASCII.GetString(new[] { re });
this.Invoke(new EventHandler(type));
}
catch (System.TimeoutException) { }
}
void type(object s, EventArgs e) // receive data
{
textBox4.Text += rs;
}
}
}
我尝试了 link 中的代码。这个简单的例子:
byte[] byte_array = new byte[5];
byte_array[0] = 0xAC;
byte_array[1] = 0x12;
byte_array[2] = 0xA0;
byte_array[3] = 0xC5;
byte_array[4] = 0x88;
Console.WriteLine(Utils.HexDump(byte_array, byte_array.Length));
产生以下输出:
重要的是您指定应在 1 行中打印数组中的多少字节。
您需要将 SerialPort
中的数据转换为字节数组。我建议尝试以下代码:
SerialPort serialPort1 = new SerialPort();
byte[] byte_array = new byte[1024];
int counter = 0;
while (serialPort1.BytesToRead > 0)
{
byte_array[counter] = (byte)serialPort1.ReadByte();
counter++;
}
编辑
为了能够使用来自 Link 的代码,向项目添加一个新的 class 文件。并将代码复制粘贴到其中。确保 *.cs class 文件与您的 Form1
class.
编辑 2:
如果您收到字符串形式的十六进制数字。您可以自己将其转换为字节数组,然后使用 link 中的 Utils
class 来显示它。这是完成此操作的一个小示例代码:
string rs = "0004"; // example from your code
byte[] byte_array = new byte[rs.Length/2]; // array is half as long as your string
int idx = 0; // count variable to index the string
int cnt = 0; // count variable to index the byte array
while (idx < rs.Length-1)
{
byte_array[cnt] = (byte)Convert.ToInt32(rs.Substring(idx, 2), 16);
idx += 2;
cnt++;
}
然后你可以这样打印结果:
textBox4.Text += Utils.HexDump(byte_array, cnt) + Environment.NewLine;