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; } //十进制转八进制---www.bianceng.cn 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));//十转十六进制 } } }
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索string
, int
, 进制之间的转换
, tostring
, textbox
, system
, 十进制
, c进制转换算法
, string转int
, 二进制 C
16进制数
c站、c语言、cf、ch、c罗,以便于您获取更多的相关知识。
时间: 2024-12-23 12:09:14