C # Implementation of decimal number conversion to binary, octal, hexadecimal number algorithm

Source: Internet
Author: User
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 ();   
            //Decimal Turn two public string Dtob (int d) {string b = "";   
            To determine if the number is less than 2, direct output 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;   
            }//Decimal octal---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;   
            }//decimal hexadecimal public string Dtox (int d) {string x = "";   
            if (d <) {x = Chang (d);   
                else {int C;   
                int s = 0;   
                int n = d;   
                int temp = D;   
                    while (n >=) {s++;   
                n = n/16;   
                } string [] m = new String[s];   
                int i = 0;   
            do {        c = D/16;   
                m[i++] = Chang (d% 16),//judge whether is greater than 10, if greater than 10, then convert to a~f format d = c;   
                while (c >= 16);   
                x = Chang (d);   
                for (int j = m.length-1 J >= 0; j--) {x = = M[j];   
        } return x; //Determine if the number is between 10~15 and if yes convert 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 (Co Nvert.   
        ToInt32 (TextBox1.Text));//10 conversion binary} private void Button2_Click (object sender, EventArgs e) {TextBox2.Text = Dtoo (Convert.ToInt32 (TextBox1.Text));//10 ext octal} private void B Utton3_click (object sender, EventArgs e) {TextBox2.Text = Dtox (Convert.ToInt32 (TextBox1.Text)); Decimal Hexadecimal}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.