hex转化为ascii c语言,如何将HEX转换为ASCII并仅从中获取数字?

我正在从秤(串行端口)接收数据为十六进制。我想将它转换成ASCII码,只需要它的重量。 代码:如何将HEX转换为ASCII并仅从中获取数字?

private void button1_Click(object sender, EventArgs e)

{

if (_serialPort != null && _serialPort.IsOpen)

_serialPort.Close();

if (_serialPort != null)

_serialPort.Dispose();

_serialPort.BaudRate = Convert.ToInt16(cbBaud.Text);

_serialPort = new SerialPort(comboBox1.Text, BaudRate, Parity.None, 8, StopBits.One);

_serialPort.DataReceived += SerialPortOnDataReceived;

_serialPort.Open();

textBox1.Text = string.Format("Listening on {0}...", comboBox1.Text);

}

private delegate void Closure();

private void SerialPortOnDataReceived(object sender, SerialDataReceivedEventArgs serialDataReceivedEventArgs)

{

if (InvokeRequired)

BeginInvoke(new Closure(() => { SerialPortOnDataReceived(sender, serialDataReceivedEventArgs); }));

else

{

while (_serialPort.BytesToRead > 0)

{

textBox1.Text += string.Format("{0:X2} ", _serialPort.ReadByte());

}

}

这是我的输出: 上侦听COM1 ... 20 20 20 30 0D 28 02 71 70 30 20 20 20 20 38 30 20 20 20 20 20 30 0D 28 02 71 70 30 20 20 20 20 38 30 20 20 20 20 20 30 0D 28 02 71 70 30 20 20 20 20 38 30 20 20 20 20 20 30 0D 28 02 71 70 30 20 20 20 20 38 30 20 20 20 20 20 30 0D 28 02 71 70 30 20 20 20 20 38 30 20 20 20 20 20 30 0D 28 02 71 70 30 20 20 20 20 38 30 20 20 20 20 20 30 0D 28 02 71 70 30 20 20 20 20 38 30 20 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

+1

你有个字节数组,所以你可以使用这个:System.Text.Encoding.SomeEncoding.GetString(字节) –

你可能感兴趣的:(hex转化为ascii,c语言)