码迷,mamicode.com
首页 > 其他好文 > 详细

进制转换

时间:2015-05-30 22:40:17      阅读:307      评论:0      收藏:0      [点我收藏+]

标签:

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

namespace 进制转换
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string s = shizhuanqita(int.Parse(textBox_10.Text),2);
            textBox_2.Text = s;
            string s8 = shizhuanqita(int.Parse(textBox_10.Text), 8);
            textBox_8.Text = s8;
            string s16 = shizhuanqita(int.Parse(textBox_10.Text), 16);
            textBox_16.Text = s16;
        }
        public string shizhuanqita(int a,int jinzhi)//输入参数为十进制数,和需要转换为几进制
        {
            string jieguo="";//定义变量,return
            while (true)
            {
                string s="";
                if (jinzhi==2||jinzhi==8)
                {
                    s = (a % jinzhi).ToString();//十进制数%进制取余
                    jieguo = s + jieguo;
                    a = a / jinzhi;//十进制数/进制到0结束 跳出循环
                    if (a==0)
                    {
                        break;
                    }
                }
                else//转换十六进制
                {
                    int b = a % jinzhi;//判断取余数的大小
                    if (b==10)
                    {
                        s = "A";
                    }
                    else if (b==11)
                    {
                        s = "B";
                    }
                    else if (b == 12)
                    {
                        s = "C";
                    }
                    else if (b == 13)
                    {
                        s = "D";
                    }
                    else if (b == 14)
                    {
                        s = "E";
                    }
                    else if (b == 15)
                    {
                        s = "F";
                    }
                    else
                    {
                        s = b.ToString();
                    }
                    jieguo = s + jieguo;
                    a = a / jinzhi;
                    if (a==0)
                    {
                        break;
                    }
                }
            }
            return jieguo;
        }

        /// <summary>
        /// 二进制转换其他进制。先把二进制转换成了十进制,然后拿十进制的数去转换八进制和十六进制
        /// </summary>
        /// <param name="a">二进制字符串</param>
        /// <param name="jinzhi">需要转换为几进制</param>
        /// <returns></returns>
        public string erjinzhizhuanqita(string a, int jinzhi)//二进制转换其他进制
        {
            int i = 1;
            string jieguo = "";
            double shijinzhi = 0;
            string s = "";
            while (true)
            {
                string b = a.Substring(a.Length - i, 1);//截取字符串
                shijinzhi = (double.Parse(b) * Math.Pow(2, i - 1)) + shijinzhi;//转换成十进制
                jieguo = shijinzhi.ToString();
                i++;
                if (i > a.Length)//i大于a的长度时跳出循环
                {
                    break;
                }
            }
            if (jinzhi == 8)
            {
                string bajinzhi="";
                while (true)
                {
                    s = (shijinzhi % jinzhi).ToString();
                    bajinzhi = s + bajinzhi;
                    jieguo = bajinzhi;
                    shijinzhi = Math.Floor(shijinzhi / jinzhi);//除完取整,不取整的话double类型的数值永远除不到0
                    if (shijinzhi == 0)
                    {
                        break;
                    }
                }
            }
            else if (jinzhi == 16)
            {
                string shiliujinzhi = "";
                while (true)
                {
                    int b = (int)shijinzhi % jinzhi;
                    if (b == 10)
                    {
                        s = "A";
                    }
                    else if (b == 11)
                    {
                        s = "B";
                    }
                    else if (b == 12)
                    {
                        s = "C";
                    }
                    else if (b == 13)
                    {
                        s = "D";
                    }
                    else if (b == 14)
                    {
                        s = "E";
                    }
                    else if (b == 15)
                    {
                        s = "F";
                    }
                    else
                    {
                        s = b.ToString();
                    }
                    shiliujinzhi = s + shiliujinzhi;
                    jieguo = shiliujinzhi;
                    shijinzhi = Math.Floor(shijinzhi / jinzhi);
                    if (shijinzhi == 0)
                    {
                        break;
                    }
                }
            }
            return jieguo;
        }
        private void button2_Click(object sender, EventArgs e)
        {
            string s10 = erjinzhizhuanqita(textBox_2.Text,10);
            textBox_10.Text = s10;
            string s8 = erjinzhizhuanqita(textBox_2.Text, 8);
            textBox_8.Text = s8;
            string s16 = erjinzhizhuanqita(textBox_2.Text, 16);
            textBox_16.Text = s16;
        }
    }
}

 

进制转换

标签:

原文地址:http://www.cnblogs.com/happinesshappy/p/4541120.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!