convert char to its decimal value in textbox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • marwanharb
    New Member
    • Mar 2015
    • 3

    convert char to its decimal value in textbox

    hello, I've designed a simple interface in C# to communicate with my FPGA (send data and receive a value), the problem is my FPGA algorithm output (ex: 3) is shown in some strange symbol (which crossbones to 0x03 in ASCII ), so it there any way to show it as 3 in textbox? thx

    Code:
    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.ToDecimal(textBox4);
            //    string ss = value.ToString();
            //    textBox5.AppendText(ss);
            //    MessageBox.Show(ss.ToString());
            //}
        }
    }
    please ignore the commented lines since I'm a beginner and was trying all sort of things, thx.
Working...