java_十进制数转换为二进制,八进制,十六进制数的算法

时间:2024-05-16 20:33:26

java_十进制数转换为二进制,八进制,十六进制数的算法

java Ê®½øÖÆÊýת»»Îª¶þ½øÖÆ,°Ë½øÖÆ,Ê®Áù½øÖÆÊýµÄË㕨 

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));//ʮתʮÁù½øÖÆ
        }
    }
}