在文本框中显示Char的十进制值

时间:2015-03-18 20:24:30

标签: c# winforms serial-port

我从FPGA接收字节并且它出现在textbox4中,但是,因为我的输出是(例如:0x03或0x07 ...),文本框中出现的是奇怪的符号(我认为是ASCII表示0x03): the interface

所以我想要的是输出只是3(十进制值0x03),我该怎么办? 我尝试了很多(参见我的代码的textbox5),但我不能,你能帮助初学者吗?谢谢。 注意:我的FPGA计算M ^ e mod G-> 33 ^ 5 mod 35 = 3 - 代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {


        public Form2()
        {
            InitializeComponent();
        }

        string RXstring = "";


        private void pictureBox2_Click(object sender, EventArgs e)
        {
            serialPort1.Close();
            Form1 myForm = new Form1();
            this.Close();


        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                if (!serialPort1.IsOpen)
                {

                    serialPort1.Open();
                    button3.Enabled = false;
                }
                else
                {
                    MessageBox.Show("Port is Open by other party!");

                }


            }
            catch (UnauthorizedAccessException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void Form2_FormClosed(object sender, FormClosedEventArgs e)
        {
            serialPort1.Close();
        }

        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            try
            {
                RXstring = serialPort1.ReadExisting();
                this.Invoke(new EventHandler(displaytext));
            }
            catch (System.TimeoutException)
            {

            }

            }
        private void displaytext(object s, EventArgs e)
        {
            //var a = RXstring[0];
            //var b = RXstring[1];
            //string h = String.Format("{0:X2}", RXstring);
             textBox4.AppendText(RXstring);


        }

        void button1_Click(object sender, EventArgs e)
        {
            //Get the strings (text)
            string textM = textBox1.Text;
            string textE = textBox2.Text;
            string textG = textBox3.Text;

            //Assuming you want unsigned numbers, convert to numeric types
            //You might want to put in exception handling for invalid inputs, watch for overflows etc.
            UInt16 bitsX = Convert.ToUInt16(0x1F01);
            UInt16 bitsM = Convert.ToUInt16(textM);
            UInt16 bitsE = Convert.ToUInt16(textE);
            UInt16 bitsG = Convert.ToUInt16(textG);


            /*
             * BitConverter puts the LSB at index 0, so depending on how you need to send the data,
             * you might want to reverse the bytes BitConverter.GetBytes(bitsM).Reverse();
             * or reverse the order you add them to the list
             */
            var byteList = new List<byte>();
            byteList.AddRange(BitConverter.GetBytes(bitsX));
            byteList.AddRange(BitConverter.GetBytes(bitsM));
            byteList.AddRange(BitConverter.GetBytes(bitsE));
            byteList.AddRange(BitConverter.GetBytes(bitsG));

            //Debugging message, uses LINQ 
            string bits = String.Join(" ",byteList.Select(b => b.ToString("X2")));
            MessageBox.Show(bits);


            //write the bytes
            var bitArray = byteList.ToArray();
            serialPort1.Write(bitArray, 0, 8);
            //var x = bitArray[1] + (bitArray[2] << 0) + (bitArray[3] << 0) + (bitArray[4] << 0) + (bitArray[5] << 0) + (bitArray[6] << 0) + (bitArray[7] << 0);

            //string y = String.Format("{0:X2}", x);
            //string y = System.Text.Encoding.UTF8.GetString(bitArray);
            //textBox5.AppendText(y);

        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
        }

        private void textBox5_TextChanged(object sender, EventArgs e)
        {
            var value = Convert.ToInt16(textBox4);
            string ss = value.ToString();

            MessageBox.Show(ss.ToString());
            string stringValue = Char.ConvertFromUtf32(value);

            decimal vOut = Convert.ToDecimal(textBox4);
            textBox5.AppendText(vOut.ToString());
        }
    }
}

3 个答案:

答案 0 :(得分:1)

您的代码尝试将十六进制字符串(如:1234567890ABCDEF)转换为字节数组。查看它的简化版本

string M = "2A";
byte b = Convert.ToByte((M.Substring(0, 2)), 16);

将以十进制显示42

您可以使用.NET中的内置类轻松完成此操作

byte[] data = SoapHexBinary.Parse(M).Value;

PS: SoapHexBinary System.Runtime.Remoting.Metadata.W3cXsd2001命名空间

答案 1 :(得分:1)

我将逻辑分解为几个步骤:

  1. 获取文字
  2. 将文本解析为数字(二进制​​)
  3. 将字节添加到列表中。
  4. List<byte>隐藏到阵列并发送到串口
  5. 我已对以下部分发表了评论。我使用ConvertBitConverter来帮助转换。此代码特意详细,以帮助显示详细信息。

    void button1_Click(object sender, EventArgs e)
    {
        //Get the strings (text)
        string textM = textBox1.Text;
        string textE = textBox2.Text;
        string textG = textBox3.Text;
    
        //Assuming you want unsigned numbers, convert to numeric types
        //You might want to put in exception handling for invalid inputs, watch for overflows etc.
        UInt16 bitsM = Convert.ToUInt16(textM);
        byte bitsE = Convert.ToByte(textE);
        UInt32 bitsG = Convert.ToUInt32(textG);
    
    
        /*
         * BitConverter puts the LSB at index 0, so depending on how you need to send the data,
         * you might want to reverse the bytes BitConverter.GetBytes(bitsM).Reverse();
         * or reverse the order you add them to the list
         */
        var byteList = new List<byte>();
        byteList.AddRange(BitConverter.GetBytes(bitsM));
        byteList.AddRange(BitConverter.GetBytes(bitsE));
        byteList.AddRange(BitConverter.GetBytes(bitsG));
    
        //Debugging message, uses LINQ 
        string bits = String.Join(" ", byteList.Select(b => b.ToString("X2")));
        MessageBox.Show(bits);
    
    
        //write the bytes
        var bitArray = byteList.ToArray();
        serialPort1.Write(bitArray, 0, 7);
    }
    

答案 2 :(得分:-2)

在某个点的for循环中,x + 2变得大于字符串M的长度,从而导致错误。 要摆脱它,将第一个for循环更改为:

 for (i = 3; i < 13; i++)
 {
      if(x + 2 <= M.Length)
      {
          data[i + 1] = Convert.ToByte((M.Substring(x, 2)), 16);
          x += 2;
      }
  }

编辑 -
你提到输入是1s和0s的形式,这使得它是二进制的,在这种情况下将上面改为:

Convert.ToByte((M.Substring(x, 2)), 2);

<小时/> 正如IV4正确指出的那样更新 - 您正在尝试将二进制字符串转换为字节,但代码意味着将十六进制字符串转换为字节 需要修改代码以便相应地工作。

 for (i = 3; i < 13; i++)
     {
          if(x + 8 <= M.Length)
          {
              data[i + 1] = Convert.ToByte((M.Substring(x, 8)), 2);
              x += 8;
          }
      }