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

Show the decimal value of Char in textbox

我从 FPGA 接收到一个字节,它出现在文本框 4 中,但是,由于我的输出是(例如:0x03 或 0x07...)文本框中出现的是奇怪的符号(我相信这是 ASCII 表示0x03):

所以我想要的输出只是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());
        }
    }
}

在 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);


Update 正如 IV4 正确指出的那样 - 您正在尝试将二进制字符串转换为字节,但代码旨在将十六进制字符串转换为字节 需要修改代码以相应地工作。

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

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

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

这将为您提供十进制的 42

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

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

PS: SoapHexBinarySystem.Runtime.Remoting.Metadata.W3cXsd2001 命名空间中

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

  1. 获取文本
  2. 将文本解析为数字(二进制​​)
  3. 将字节添加到列表中。
  4. List<byte>转换成数组发送到串口

我对以下部分进行了评论。我使用 Convert and BitConverter 来帮助进行转换。此代码故意冗长以帮助显示详细信息。

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);
}