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


    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));//ʮתʮÁù½øÖÆ
            }
        }
    }
  • 相关阅读:
    高级特性(7)- 高级AWT
    洛谷 P1948 [USACO08JAN]电话线Telephone Lines
    洛谷 P2015 二叉苹果树
    洛谷 P2014 选课
    洛谷 P1560 [USACO5.2]蜗牛的旅行Snail Trails(不明原因的scanf错误)
    cogs 10. 信号无错传输
    cogs 9. 中心台站建设。。。
    洛谷 P1731 生日蛋糕
    洛谷 P1092 虫食算
    洛谷 P1034 矩形覆盖
  • 原文地址:https://www.cnblogs.com/timssd/p/4203313.html
Copyright © 2020-2023  润新知