C# 如何将十六进制转换为ASCII码并仅从中获取数字?

C# 如何将十六进制转换为ASCII码并仅从中获取数字?,c#,hex,ascii,scale,C#,Hex,Ascii,Scale,我从称重秤(串行端口)接收十六进制数据。我想把它转换成ASCII,只需要它的重量。 代码: 这是我的输出: 听COM1…20 20 20 20 30 0D 28 02 71 70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20 20 20 30 20 20 30 30 20 20 20 20 30 D 28 02 71 70 20 20 20 20 20 20 20 20 20 20 20 20 20 30 D 28 02 71 70 20

我从称重秤(串行端口)接收十六进制数据。我想把它转换成ASCII,只需要它的重量。 代码:

这是我的输出: 听COM1…20 20 20 20 30 0D 28 02 71 70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20 20 20 30 20 20 30 30 20 20 20 20 30 D 28 02 71 70 20 20 20 20 20 20 20 20 20 20 20 20 20 30 D 28 02 71 70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30 D 28 02 71 70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2020 30 0D 28 02 71 70 30 20 20 20 38 30 20 20

这是我从网站手动转换的ASCII代码:

  0 
  (  q p 0         8 0           0 
  (  q p 0         8 0           0 
  (  q p 0         8 0           0 
  (  q p 0         8 0           0 
  (  q p 0         8 0           0 
  (  q p 0         8 0           0 
  (  q p 0         8 0      

您已经复制了一些expample,但是有更合适的方法从串行端口读取,这更适合您的目的

首先,您需要从手册中确定加权指示器的数据格式。然后您可以使用这个(而不是while循环):

其中
“\r”
重量记录分隔符(也可以是其他字符串,如
等)

如果要从串行端口缓冲区读取所有可用的输入数据,可以使用

string myData = _serialPort.ReadExisting();
并按指定的字符拆分字符串

string[] weightRecords = myData.Split('\r');
然后从获得的字符串中解析权重数值。因此,您不需要从字节转换数据,您可以将其作为字符串直接读取。如果你需要更多的灵感,检查一下


如果您想尝试,可以重写DataReceived处理程序的主体,如下所示:

    if (InvokeRequired)     
        BeginInvoke(new Closure(() => { SerialPortOnDataReceived(sender, serialDataReceivedEventArgs); }));    
    else
    {
         //There may be multiple records available in the Serial Port buffer
         string myData = _serialPort.ReadExisting();
         //Suppose that 0D character (equals to \r) marks the end of record, see manual of the indicator
         string[] weightRecords = myData.Split('\r');
         foreach (var s in weightRecords)
         {
              //9,5 = Position and length of the numeric weight value, for exact values see your weight indicator manual
              textBox1.Text += string.Format("{0} kg \n", s.Substring(9,5)); 
         }
    }
string[] stringSeparators = new string[] {
  System.Text.Encoding.ASCII.GetString(new byte[] {0x0d, 0x28})
};
string[] weightRecords = myData.Split(stringSeparators, StringSplitOptions.None);
要使用由多个字符组成的分隔符拆分字符串,可以使用以下方法:

    if (InvokeRequired)     
        BeginInvoke(new Closure(() => { SerialPortOnDataReceived(sender, serialDataReceivedEventArgs); }));    
    else
    {
         //There may be multiple records available in the Serial Port buffer
         string myData = _serialPort.ReadExisting();
         //Suppose that 0D character (equals to \r) marks the end of record, see manual of the indicator
         string[] weightRecords = myData.Split('\r');
         foreach (var s in weightRecords)
         {
              //9,5 = Position and length of the numeric weight value, for exact values see your weight indicator manual
              textBox1.Text += string.Format("{0} kg \n", s.Substring(9,5)); 
         }
    }
string[] stringSeparators = new string[] {
  System.Text.Encoding.ASCII.GetString(new byte[] {0x0d, 0x28})
};
string[] weightRecords = myData.Split(stringSeparators, StringSplitOptions.None);

您有字节数组,因此可以使用:System.Text.Encoding.SomeEncoding.GetString(字节)谢谢您的帮助。事实上,我对编程很陌生,只是想找到一些代码并编辑它们。我应该只删除while循环还是删除整个if和else块?