在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#语言基础 → C# 十进制、十六进制、二进制转换 示例(写的比较乱)

C# 十进制、十六进制、二进制转换 示例(写的比较乱)

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:0.13M
  • 下载次数:72
  • 浏览次数:448
  • 发布时间:2016-04-28
  • 实例类别:C#语言基础
  • 发 布 人:miao27989
  • 文件格式:.zip
  • 所需积分:4
 相关标签: 内有多种算法和规则

实例介绍

【实例简介】窗体自动及窗体内控制随着自适应变化。//字节数组转16进制字符串.字节数组转16进制字符串.//字符串转换成数组.// 返回十六进制代表的字符串 ,字符串转换为数组.字符串转换为数组.类库DLL。数据库连接字段DLL文件。

【实例截图】

【核心代码】

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Collections;

namespace WindowsFormsApplication2._27
{
    public partial class Form1 : Form
    {
        SqlHelpers.Class1 sqlhelper = new SqlHelpers.Class1();
        public static SqlDataReader sqlread { get; set; }
        AutoSizeFormClass asc = new AutoSizeFormClass();
        AutoSizeForm asf = new AutoSizeForm();
        Class1 asdb = new Class1();
        resizeFrm resize = new resizeFrm();

        AutoSizeFormClass3 asfc = new AutoSizeFormClass3();      //3.7
        int X;
        int Y;

        private ArrayList InitialCrl = new ArrayList();//用以存储窗体中所有的控件名称
        private ArrayList CrlLocationX = new ArrayList();//用以存储窗体中所有的控件原始位置
        private ArrayList CrlLocationY = new ArrayList();//用以存储窗体中所有的控件原始位置
        private ArrayList CrlSizeWidth = new ArrayList();//用以存储窗体中所有的控件原始的水平尺寸
        private ArrayList CrlSizeHeight = new ArrayList();//用以存储窗体中所有的控件原始的垂直尺寸
        private int FormSizeWidth;//用以存储窗体原始的水平尺寸
        private int FormSizeHeight;//用以存储窗体原始的垂直尺寸

        private double FormSizeChangedX;//用以存储相关父窗体/容器的水平变化量
        private double FormSizeChangedY;//用以存储相关父窗体/容器的垂直变化量 

        private int Wcounter = 0;//为防止递归遍历控件时产生混乱,故专门设定一个全局计数器

        public Form1()
        {
            InitializeComponent();
            this.Resize  = new EventHandler(Form1_Resize); //3.12
            resize.firstDemo(this);        //3.12
            #region
            //int count = this.Controls.Count * 2   2;
            //float[] factor = new float[count];
            //int i = 0;
            //factor[i  ] = Size.Width;
            //factor[i  ] = Size.Height;
            //foreach (Control ctrl in this.Controls)
            //{
            //    factor[i  ] = ctrl.Location.X / (float)Size.Width;
            //    factor[i  ] = ctrl.Location.Y / (float)Size.Height;
            //    ctrl.Tag = ctrl.Size;
            //}
            //Tag = factor;
            //asc.controllInitializeSize(this);
            //string A = "10000";
            //string B = "0000";
            //string C = A   B;
            //textBox1.Text = C;
            //label1.Text = C;
            //sqlread = sqlhelper.getread
            //       ("select Equipments_id, PT_name  from Equipments_id");
            //while (sqlread.Read())
            //{
            //    comboBox1.Items.Add((string)sqlread["Equipments_id"].ToString().Trim());
            //    //Frmdata.frm_cmbxsbh
            //}
            #endregion
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string A = "yd";
            string B = "0003";
            string C = A   B;
            textBox1.Text = C;
            //label1.Text = C;
            //comboBox1.Items.Add("c");
          
            sqlread = sqlhelper.getread
                   ("select Equipments_id, PT_name  from Equipments_id");
            while (sqlread.Read())
            {
                comboBox1.Items.Add((string)sqlread["Equipments_id"].ToString().Trim());
                //Frmdata.frm_cmbxsbh
            }
            if (comboBox1.Items.Contains(label1.Text.ToString()))
            {
                MessageBox.Show("yes");
            }
            comboBox1.SelectedIndex = 0;
            textBox6.Text = comboBox1.Text.Trim();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //asc.controllInitializeSize(this);
            //asf.controllInitializeSize(this);
            //asf.controlAutoSize(this);
            //asdb.controlAutoSize(this);
            //asdb.controllInitializeSize(this);      
            //asfc.controllInitializeSize(this);             //3.7
         
            //GetInitialFormSize();           
            //GetAllCrlLocation(this);
            //GetAllCrlSize(this);

            //this.Resize  = new EventHandler(Form1_Resize);//窗体调整大小时引发事件
            //X = this.Width;//获取窗体的宽度
            //Y = this.Height;//获取窗体的高度
            //setTag(this);//调用方法
            this.TBsystemtwo.KeyPress  = new KeyPressEventHandler(textBox22_KeyPress);  
        }
        public void GetAllCrlLocation(Control CrlContainer)//获得并存储窗体中各控件的初始位置
        {
            foreach (Control iCrl in CrlContainer.Controls)
            {

                if (iCrl.Controls.Count > 0)
                    GetAllCrlLocation(iCrl);
                InitialCrl.Add(iCrl);
                CrlLocationX.Add(iCrl.Location.X);
                CrlLocationY.Add(iCrl.Location.Y);


            }
        }
        public void GetAllCrlSize(Control CrlContainer)//获得并存储窗体中各控件的初始尺寸
        {
            foreach (Control iCrl in CrlContainer.Controls)
            {
                if (iCrl.Controls.Count > 0)
                    GetAllCrlSize(iCrl);
                CrlSizeWidth.Add(iCrl.Width);
                CrlSizeHeight.Add(iCrl.Height);
            }
        }

        public void GetInitialFormSize()//获得并存储窗体的初始尺寸
        {

            FormSizeWidth = this.Size.Width;
            FormSizeHeight = this.Size.Height;

        } 

        private void setTag(Control cons)
        {
            //遍历窗体中的控件
            foreach (Control con in cons.Controls)
            {
                con.Tag = con.Width   ":"   con.Height   ":"   con.Left   ":"   con.Top   ":"   con.Font.Size;
                if (con.Controls.Count > 0)
                    setTag(con);
            }
        }
        private void setControls(float newx, float newy, Control cons)
        {
            //遍历窗体中的控件,重新设置控件的值
            foreach (Control con in cons.Controls)
            {
                string[] mytag = con.Tag.ToString().Split(new char[] { ':' });//获取控件的Tag属性值,并分割后存储字符串数组
                float a = Convert.ToSingle(mytag[0]) * newx;//根据窗体缩放比例确定控件的值,宽度
                con.Width = (int)a;//宽度
                a = Convert.ToSingle(mytag[1]) * newy;//高度
                con.Height = (int)(a);
                a = Convert.ToSingle(mytag[2]) * newx;//左边距离
                con.Left = (int)(a);
                a = Convert.ToSingle(mytag[3]) * newy;//上边缘距离
                con.Top = (int)(a);
                Single currentSize = Convert.ToSingle(mytag[4]) * newy;//字体大小
                con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);
                if (con.Controls.Count > 0)
                {
                    setControls(newx, newy, con);
                }
            }
        }

        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            //asc.controllInitializeSize(this);
            //asdb.controlAutoSize(this);
            //asfc.controlAutoSize(this);       //3.7

            // MessageBox.Show("窗体尺寸改变");
            //Wcounter = 0;
            //int counter = 0;
            //if (this.Size.Width < FormSizeWidth || this.Size.Height < FormSizeHeight)
            ////如果窗体的大小在改变过程中小于窗体尺寸的初始值,则窗体中的各个控件自动重置为初始尺寸,且窗体自动添加滚动条
            //{

            //    foreach (Control iniCrl in InitialCrl)
            //    {
            //        iniCrl.Width = (int)CrlSizeWidth[counter];
            //        iniCrl.Height = (int)CrlSizeHeight[counter];
            //        Point point = new Point();
            //        point.X = (int)CrlLocationX[counter];
            //        point.Y = (int)CrlLocationY[counter];
            //        iniCrl.Bounds = new Rectangle(point, iniCrl.Size);
            //        counter  ;
            //    }
            //    this.AutoScroll = true;
            //}
            //else
            ////否则,重新设定窗体中所有控件的大小(窗体内所有控件的大小随窗体大小的变化而变化)
            //{
            //    this.AutoScroll = false;
            //    ResetAllCrlState(this);
            //}

        }
        public void ResetAllCrlState(Control CrlContainer)//重新设定窗体中各控件的状态(在与原状态的对比中计算而来)
        {
            FormSizeChangedX = (double)this.Size.Width / (double)FormSizeWidth;
            FormSizeChangedY = (double)this.Size.Height / (double)FormSizeHeight;
            foreach (Control kCrl in CrlContainer.Controls)
            {
                /*string name = kCrl.Name.ToString();
                MessageBox.Show(name);
                MessageBox.Show(Wcounter.ToString());*/
                if (kCrl.Controls.Count > 0)
                {
                    ResetAllCrlState(kCrl);

                }
                Point point = new Point();
                point.X = (int)((int)CrlLocationX[Wcounter] * FormSizeChangedX);
                point.Y = (int)((int)CrlLocationY[Wcounter] * FormSizeChangedY);
                kCrl.Width = (int)((int)CrlSizeWidth[Wcounter] * FormSizeChangedX);
                kCrl.Height = (int)((int)CrlSizeHeight[Wcounter] * FormSizeChangedY);
                kCrl.Bounds = new Rectangle(point, kCrl.Size);
                Wcounter  ;
                // MessageBox.Show(Wcounter.ToString());
            }
        } 

        /// <summary>
        /// 字符串转换成数组
        /// </summary>
        /// <param name="shex"></param>
        /// <returns></returns>
        public static byte[] GetByteArray(string shex)  //字符串转换成数组.
        {
            string[] ssArray = shex.Split(' ');
            List<byte> bytList = new List<byte>();
            foreach (var s in ssArray)
            {
                //将十六进制的字符串转换成数值
                bytList.Add(Convert.ToByte(s, 16));
            }
            //返回字节数组
            return bytList.ToArray();
        }
        /// <summary>
        /// 字符串转换为数组.
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        static byte[] GetBytes(string str)    //字符串转换为数组.
        {
            byte[] bytes = new byte[str.Length/2 * sizeof(char)];
            System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
            return bytes;
        }
        /// 十六进制换算为十进制
        /// </summary>
        /// <param name="strColorValue"></param>
        /// <returns></returns>
        public static int GetHexadecimalValue(String strColorValue)
        {
            char[] nums = strColorValue.ToCharArray();
            int total = 0;
            try
            {
                for (int i = 0; i < nums.Length; i  )
                {
                    String strNum = nums[i].ToString().ToUpper();
                    switch (strNum)
                    {
                        case "A":
                            strNum = "10";
                            break;
                        case "B":
                            strNum = "11";
                            break;
                        case "C":
                            strNum = "12";
                            break;
                        case "D":
                            strNum = "13";
                            break;
                        case "E":
                            strNum = "14";
                            break;
                        case "F":
                            strNum = "15";
                            break;
                        default:
                            break;
                    }
                    double power = Math.Pow(16, Convert.ToDouble(nums.Length - i - 1));
                    total  = Convert.ToInt32(strNum) * Convert.ToInt32(power);
                }

            }
            catch (System.Exception ex)
            {
                String strErorr = ex.ToString();
                return 0;
            }
            return total;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int a = Convert.ToInt32(textBox2.Text.Trim().ToString());
            string b=textBox3.Text = a.ToString("X8").ToString();           
            byte[] bufferdata1 = new byte[4];
             //byte[] bufferdata = GetByteArray(d);
            for (int i = 0; i < 4; i  )
            {
              string  d = b.ToString().Substring(i*2,2);
                byte[] bufferdata=GetByteArray(d);
                bufferdata1[i] = bufferdata[0];
            }

            //bufferdata[0] =0xd;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string opentxt = "2016-03-05/15:35:47";
            string stoptxt = "2016-03-05/09:23:47";
            SqlDataReader sqlread = sqlhelper.getread
                        ("select EquipmentsClose,EquipmentsOpen from Equipments_activation_record "   " where EquipmentsOpen='"   opentxt   "' and EquipmentsClose='"   stoptxt   "'");
            sqlread.Read();    
            if(!sqlread.HasRows)
            {
                MessageBox.Show("没有相同数据", "提示",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("有相同数据", "提示",
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Information);
            }
        }

        private void button9_Click(object sender, EventArgs e)
        {
            int a = Convert.ToInt32(textBox4.Text.Trim().ToString());
            string b = a.ToString("X8").ToString();
            byte[] array = System.Text.Encoding.ASCII.GetBytes(b.Trim());
            //byte[] array = System.Text.Encoding.ASCII.GetBytes(textBox4.Text.Trim());
            //string A=System.Text.Encoding.ASCII.GetString(textBox4.Text.Trim());
            string str = null;
            string strt = null;
            for (int i = 0; i < array.Length; i  )
            {
                if (i < array.Length/2)
                {
                    string asciicode = array[i].ToString("x2");
                    str  = Convert.ToString(asciicode)   " ";
                }
                else
                {
                    string asciicode = array[i].ToString("x2");
                    strt  = Convert.ToString(asciicode)   " ";
                }
            }
            textBox5.Text = strt str; // 97,98,99
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBox6.Text = comboBox1.Text.Trim();
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            resize.resizeStatus(this);
            //float[] scale = (float[])Tag;
            //int i = 2;

            //foreach (Control ctrl in this.Controls)
            //{
            //    ctrl.Left = (int)(Size.Width * scale[i  ]);
            //    ctrl.Top = (int)(Size.Height * scale[i  ]);
            //    ctrl.Width = (int)(Size.Width / (float)scale[0] * ((Size)ctrl.Tag).Width);
            //    ctrl.Height = (int)(Size.Height / (float)scale[1] * ((Size)ctrl.Tag).Height);

            //    //每次使用的都是最初始的控件大小,保证准确无误。
            //}
            //float newx = (this.Width) / X; //窗体宽度缩放比例
            //float newy = this.Height / Y;//窗体高度缩放比例
            //setControls(newx, newy, this);//随窗体改变控件大小
            //this.Text = this.Width.ToString()   " "   this.Height.ToString();//窗体标题栏文本
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            textBox7.Text = "已选种";    
        }

        private void button10_Click(object sender, EventArgs e)
        {
            int a = Convert.ToInt32(textBox4.Text.Trim().ToString());
            string b = a.ToString("X8").ToString();

            byte[] array = System.Text.Encoding.ASCII.GetBytes(b.Trim());
            //byte[] array = System.Text.Encoding.ASCII.GetBytes(textBox4.Text.Trim());
            //string A=System.Text.Encoding.ASCII.GetString(textBox4.Text.Trim());
            string str = null;
            for (int i = 0; i < array.Length; i  )
            {
                    string asciicode = array[i].ToString("x2");
                    str  = Convert.ToString(asciicode)   " ";
               
            }
            textBox8.Text =str; // 97,98,99
        }

        private void button11_Click(object sender, EventArgs e)
        {
           int c= Convert.ToInt32(textBox9.Text.Trim(), 16);
           textBox10.Text = c.ToString();
        }

        private void button8_Click(object sender, EventArgs e)
        {
        //    string rc="00000000";
        //    string ac = "00";
        //    string ae = "1010";
        //    string ace = Dataconversion.GetHexadecimalValue(ac ae).ToString();
        //    if (ace.Length < 8)
        //    {
        //        ace = rc.Substring(0, 8 - ace.Length)   ace;       
        //    }
        //    this.textBox11.Text = ace;
            int  dcad = Convert.ToInt32(textBox11.Text.Trim());
            textBox12.Text = Convert.ToString(dcad,2).ToString();
        }

        private void button12_Click(object sender, EventArgs e)
        {
            string btnso = "000000000010000";
            byte[] arry = new byte[btnso.Length];
            char[] arr = btnso.ToCharArray();
            for (int i = 0; i < arr.Length;i   )
            {
                arry[i] = Convert.ToByte(arr[i].ToString());                
            }
            textBox13.Text = string.Join("",arry); //数组转字符串。
            textBox14.Text = Convert.ToInt32(textBox13.Text.Trim(),2).ToString(); //二进制转十进制数。
            int c = Convert.ToInt32(string.Format("{0:x}", Convert.ToInt32(string.Join("", arry), 2)));
            textBox18.Text = string.Format("{0:x}", Convert.ToInt32(string.Join("", arry), 2));//二进制转16进制。
            //string cc = arr[0].ToString();
            //arr[1] ='1';
        }

        private void button13_Click(object sender, EventArgs e)
        {
           int  c =Convert.ToInt32(textBox15.Text.Trim().ToString());
           int b = Convert.ToInt32(textBox16.Text.Trim().ToString());
           int d = c | b;

           textBox17.Text = d.ToString();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            try
            {
                Double d=0, m=0, f = 0, x = 0, x1 = 0, sf, sc, sc1, sc2;
                d = Convert.ToDouble(TBMachinetwo.Text.Trim())   Convert.ToDouble(TBDirtytwo.Text.Trim())   Convert.ToDouble(TBflowstwo.Text.Trim());  //秒的总数和  
                if (d >= 60)
                {
                    f = Convert.ToInt32(d / 60);
                    //m = d % 60;
                    //x = Math.Round((d % 60) / 60, 2);
                }
                    x = Math.Round((d % 60) / 60, 2);
                  sc2 = Convert.ToDouble(TBMachineone.Text.Trim())   Convert.ToDouble(TBDirtyone.Text.Trim())   Convert.ToDouble(TBflowsone.Text.Trim())   f   x;  //分步设置时间总和 
                ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                if (Convert.ToInt32(TBsystemtwo.Text.Trim()) <= 60)            //总的 秒时间 
                {
                    sf = Convert.ToDouble(TBsystemtwo.Text.Trim()) % 60;
                    x1 = Math.Round((Convert.ToDouble(TBsystemtwo.Text.Trim()) % 60) / 60, 2);
                }
                sc1 = Convert.ToDouble(TBsystemone.Text.Trim())   x1; //总设置时间
                ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

                if (Convert.ToInt32(TBInsideone.Text.Trim())   Convert.ToInt32(TBInsidetwo.Text.Trim()) < Convert.ToInt32(TBInsidetimeO.Text.Trim())   Convert.ToInt32(TBInsidetimeT.Text.Trim()))
               {
                   MessageBox.Show("输入非法,总设置时间小于以下的和TBInsideone!!!");
                }                
                if (sc1 < sc2)
                {
                    MessageBox.Show("输入非法,总设置时间小于以下的和!!!");
                }

            }
            catch { 
                MessageBox.Show("框内数据不能为空"); 
             if(this.TBDirtyone.Text.Trim()=="")
             {
                 this.TBDirtyone.Focus();
             }
            }
        }

        private void textBox22_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar < '0' || e.KeyChar > '9')//&& e.KeyChar!='\b')
            {
                if (e.KeyChar != '\b')
                {
                e.Handled = true;
                MessageBox.Show("输入非法!");
                TBsystemtwo.Focus();
               }
            }           
        }

        private void textBox22_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox22_Leave(object sender, EventArgs e)
        {
            if (Convert.ToInt32(TBsystemtwo.Text.Trim())>=60)
            {
                MessageBox.Show("输入非法,请输入小于60的数字!");
                TBsystemtwo.Text = "";
                TBsystemtwo.Focus();
            }
           
        }

        private void button4_Click(object sender, EventArgs e)
        {

        }
    }
}

实例下载地址

C# 十进制、十六进制、二进制转换 示例(写的比较乱)

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警