二、八、十、十六进制相互转换
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { //string s = zhuanhuan1(int.Parse(textBox1.Text), 2);//10转2 //textBox2.Text = s; //string s1 = zhuanhuan1(int.Parse(textBox1.Text), 8);//10转8 //textBox3.Text = s1; //string s2 = zhuanhuan1(int.Parse(textBox1.Text), 16);//10转16 //textBox4.Text = s2; double s3 = zhuanhuan3(textBox4.Text, 16);//8转10 textBox1.Text = s3.ToString(); //double s4 = zhuanhuan2(textBox3.Text, 8);//8转10 //textBox1.Text = s4.ToString(); //double s4 = zhuanhuan2(textBox2.Text, 2);//2转10 //textBox1.Text = s4.ToString(); } private string zhuanhuan1(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 if(jinzhi==16) { int yushu = a % jinzhi; string b = ""; if (yushu == 10) { b = "A"; } else if (yushu == 11) { b = "B"; } else if (yushu == 12) { b = "C"; } else if (yushu == 13) { b = "D"; } else if (yushu == 14) { b = "E"; } else if (yushu == 15) { b = "F"; } else { b = yushu.ToString(); } jieguo = b + jieguo; a = a / jinzhi; if (a==0) { break; } } } return jieguo; } private double zhuanhuan2(string a, double zhuanjinzhi)//把进制为zhuanjinzhi的进制数转换为十进制 { double sum = 0; for (int i = 0; i < a.Length; i++) { sum = sum + (Convert.ToDouble(a.Substring(i, 1))) * Math.Pow(zhuanjinzhi,a.Length-i-1); } return sum; } private double zhuanhuan3(string a, double shiliu)//把进制为十六进制数转换为十进制 { double sum = 0; string c; 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 + Convert.ToDouble(c) * Math.Pow(shiliu,a.Length-i-1); } return sum; } } }
二百个不间断的重复,只是让我看到了人的命运无法改变这一事实而已。