• 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));//ʮתʮÁù½øÖÆ
            }
        }
    }
  • 相关阅读:
    WPF treeview 多层次绑定问题 HierarchicalDataTemplate 和 CompositeCollection
    Difference between Visibility.Collapsed and Visibility.Hidden
    WPF style的继承
    Asp.net个性化服务《系列03_购物车》
    SQLServer2005中的uniqueidentifier数据类型与NEWID()函数
    扉页语
    Asp.net个性化服务《系列01_概述》
    Asp.net个性化服务《系列02_快速的匿名用户个性化》
    2010年java课程体系
    easyui修改提示窗
  • 原文地址:https://www.cnblogs.com/timssd/p/4203313.html
Copyright © 2020-2023  润新知