winform 进制之间的转换

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;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //string s = shizhuan(int.Parse(textBox1.Text), 2);//十转二
            //textBox2.Text = s;
            //string s1 = shizhuan(int.Parse(textBox1.Text), 8);//十转八
            //textBox3.Text = s1;
            //string s2 = shizhuan(int.Parse(textBox1.Text), 16);//十转十六
            //textBox4.Text = s2;
            Double s4 = zhuanhuan((textBox2.Text), 2);//二转十
            textBox1.Text = s4.ToString();
            //Double s5 = zhuanhuan((textBox3.Text), 8);//八转十
            //textBox1.Text = s5.ToString();
            //Double s6 = zhuanhuan((textBox4.Text), 16);//十六转十
            //textBox1.Text = s6.ToString();

        }
        public double zhuanhuan(string a, double  jinzhi)
        {
            string c = "";
            double sum = 0;
            for (int i = 0; i <a.Length; i++)
            {
                if (a.Substring(i, 1) == "A")
                {

                    c = "10";
                }
                else if (a.Substring(i, 1) == "B")
                {

                    c= "11";
                }
                else if (a.Substring(i, 1) == "C")
                {

                    c = "12";
                }
                else if (a.Substring(i, 1) == "D")
                {

                    c = "13";
                }
                else if (a.Substring(i, 1) == "E")
                {

                    c = "14";
                }
                else if (a.Substring(i, 1) == "F")
                {

                    c = "15";
                }
                else
                {
                    c = a.Substring(i, 1);

                }
                sum = sum + (double.Parse(c)) * Math.Pow(jinzhi, a.Length-i-1);
            }
            return sum;
        }

        public string shizhuan(int a, int jinzhi)
        {
            string jieguo = "";
            while (true)
            {
                if (jinzhi==2||jinzhi==8)
                {
                    string s = (a % jinzhi).ToString();
                    jieguo = s + jieguo;
                    a = a / jinzhi;
                    if (a==0)
                    {
                        break;
                    }
                }
                else
                {
                    int yushu = a % jinzhi;
                    string s = "";
                    if (yushu==10)
                    {
                        s = "A";
                    }
                    else if (yushu == 11)
                    {
                        s = "B";
                    }
                    else if (yushu == 12)
                    {
                        s = "C";
                    }
                    else if (yushu == 13)
                    {
                        s = "D";
                    }
                    else if (yushu == 14)
                    {
                        s = "E";
                    }
                    else if (yushu == 15)
                    {
                        s = "F";
                    }
                    else
                    {
                        s = yushu.ToString();
                    }
                    jieguo = s + jieguo;
                    a = a / jinzhi;
                    if (a==0)
                    {
                        break;
                    }
                }
            }
            return jieguo;
        }
    }
}

  

转载于:https://www.cnblogs.com/Mr-xue/p/4607181.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值