二进制,八进制,十进制,十六进制互换

来源:互联网 发布:罗伯特舒曼 知乎 编辑:程序博客网 时间:2024/06/08 06:43
using System;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            RichTextBox richText = new RichTextBox();
            richText.Dock = DockStyle.Fill;
            this.Controls.Add(richText);
            this.StartPosition = FormStartPosition.CenterScreen;
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("二进制 01010101 的八进制表示: {0}", Convert.ToString(Convert.ToInt32("01010101", 2), 8));
            sb.AppendLine();
            sb.AppendFormat("二进制 01010101 的十进制表示: {0}", Convert.ToInt32("01010101", 2));
            sb.AppendLine();
            sb.AppendFormat("二进制 01010101 的十六进制表示: {0:X}", Convert.ToInt32("01010101", 2));
            sb.AppendLine();
            sb.AppendLine();
            sb.AppendFormat("八进制 125 的二进制表示: {0}", Convert.ToString(Convert.ToInt32("125", 8), 2));
            sb.AppendLine();
            sb.AppendFormat("八进制 125 的十进制表示: {0}", Convert.ToInt32("125", 8));
            sb.AppendLine();
            sb.AppendFormat("八进制 125 的十六进制表示: {0:X}", Convert.ToInt32("125", 8));
            sb.AppendLine();
            sb.AppendLine();
            sb.AppendFormat("十进制 85 的二进制表示: {0}", Convert.ToString(85, 2));
            sb.AppendLine();
            sb.AppendFormat("十进制 85 的八进制表示: {0}", Convert.ToString(85, 8));
            sb.AppendLine();
            sb.AppendFormat("十进制 168 的十六进制表示: {0:X}", 168);
            sb.AppendLine();
            sb.AppendLine();
            sb.AppendFormat("十六进制 A8 的二进制表示: {0}", Convert.ToString(0xA8, 2));
            sb.AppendLine();
            sb.AppendFormat("十六进制 A8 的八进制表示: {0}", Convert.ToString(0xA8, 8));
            sb.AppendLine();
            sb.AppendFormat("十六进制 A8 的十进制表示: {0}", Convert.ToString(0xA8, 10));
            richText.Text = sb.ToString();
            sb.Capacity = sb.Remove(0, sb.Length).Length;
            sb = null;
        }
    }
}