常用演算法(C#): 十進位數轉換為二進位,八進位,十六進位數的演算法

來源:互聯網
上載者:User

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

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

        //十進位轉二制
        public string DtoB(int d)
        {
            string b = "";
            //判斷該數如果小於2,則直接輸出
            if (d < 2)
            {
                b = d.ToString();
            }
            else
            {
                int c;
                int s = 0;
                int n = d;
                while (n >= 2)
                {
                    s++;
                    n = n / 2;
                }
                int[] m = new int[s];
                int i = 0;
                do
                {
                    c = d / 2;
                    m[i++] = d % 2;
                    d = c;
                } while (c >= 2);
                b = d.ToString();
                for (int j = m.Length - 1; j >=0; j--)
                {
                    b += m[j].ToString ();
                }
            }
            return b;
        }

        //十進位轉八進位
        public string DtoO(int d)
        {
            string o = "";
            if (d < 8)
            {
                o = d.ToString();
            }
            else
            {
                int c;
               
                int s=0;
                int n=d;
                int temp = d;
                while (n >= 8)
                {
                    s++;
                    n = n / 8;
                }
                int[] m = new int[s];
                int i = 0;
                do
                {
                    c = d / 8;
                    m[i++] = d % 8;
                    d = c;
                } while (c >= 8);
                o = d.ToString();
                for (int j = m.Length - 1; j >= 0; j--)
                {
                    o += m[j];
                }
            }
            return o;
        }

        //十進位轉十六進位
        public string DtoX(int d)
        {
            string x = "";
            if (d < 16)
            {
                x = chang(d);
            }
            else
            {
                int c;

 

 

                int s = 0;
                int n = d;
                int temp = d;
                while (n >= 16)
                {
                    s++;
                    n = n / 16;
                }
                string [] m = new string[s];
                int i = 0;
                do
                {
                    c = d / 16;
                    m[i++] = chang(d % 16);//判斷是否大於10,如果大於10,則轉換為A~F的格式
                    d = c;
                } while (c >= 16);
                x = chang(d);
                for (int j = m.Length - 1; j >= 0; j--)
                {
                    x += m[j];
                }
            }
            return x;
        }


        //判斷是否為10~15之間的數,如果是則進行轉換
        public string chang(int d)
        {
            string x = "";
            switch (d)
            {
                case 10:
                    x = "A";
                    break;
                case 11:
                    x = "B";
                    break;
                case 12:
                    x = "C";
                    break;
                case 13:
                    x = "D";
                    break;
                case 14:
                    x = "E";
                    break;
                case 15:
                    x = "F";
                    break;
                default:
                    x = d.ToString();
                    break;
            }
            return x;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox2.Text = DtoB(Convert.ToInt32(textBox1.Text));//十轉二進位
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox2.Text = DtoO(Convert.ToInt32(textBox1.Text));//十轉八進位
        }

        private void button3_Click(object sender, EventArgs e)
        {
            textBox2.Text = DtoX(Convert.ToInt32(textBox1.Text));//十轉十六進位
        }
    }
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.