标签:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { //string s = shizhuan(int.Parse(textBox1.Text), 2);//十转二 //textBox2.Text = s; //string s1 = shizhuan(int.Parse(textBox1.Text), 8);//十转八 //textBox3.Text = s1; //string s2 = shizhuan(int.Parse(textBox1.Text), 16);//十转十六 //textBox4.Text = s2; Double s4 = zhuanhuan((textBox2.Text), 2);//二转十 textBox1.Text = s4.ToString(); //Double s5 = zhuanhuan((textBox3.Text), 8);//八转十 //textBox1.Text = s5.ToString(); //Double s6 = zhuanhuan((textBox4.Text), 16);//十六转十 //textBox1.Text = s6.ToString(); } public double zhuanhuan(string a, double jinzhi) { string c = ""; double sum = 0; for (int i = 0; i <a.Length; i++) { if (a.Substring(i, 1) == "A") { c = "10"; } else if (a.Substring(i, 1) == "B") { c= "11"; } else if (a.Substring(i, 1) == "C") { c = "12"; } else if (a.Substring(i, 1) == "D") { c = "13"; } else if (a.Substring(i, 1) == "E") { c = "14"; } else if (a.Substring(i, 1) == "F") { c = "15"; } else { c = a.Substring(i, 1); } sum = sum + (double.Parse(c)) * Math.Pow(jinzhi, a.Length-i-1); } return sum; } public string shizhuan(int a, int jinzhi) { string jieguo = ""; while (true) { if (jinzhi==2||jinzhi==8) { string s = (a % jinzhi).ToString(); jieguo = s + jieguo; a = a / jinzhi; if (a==0) { break; } } else { int yushu = a % jinzhi; string s = ""; if (yushu==10) { s = "A"; } else if (yushu == 11) { s = "B"; } else if (yushu == 12) { s = "C"; } else if (yushu == 13) { s = "D"; } else if (yushu == 14) { s = "E"; } else if (yushu == 15) { s = "F"; } else { s = yushu.ToString(); } jieguo = s + jieguo; a = a / jinzhi; if (a==0) { break; } } } return jieguo; } } }
标签:
原文地址:http://www.cnblogs.com/Mr-xue/p/4607181.html