程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> C#實現十進制數轉換為二進制、八進制、十六進制數的算法

C#實現十進制數轉換為二進制、八進制、十六進制數的算法

編輯:關於C#
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;   
        }   
  
        //十進制轉八進制---www.bianceng.cn
        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));//十轉十六進制   
        }   
    }   
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved