Common algorithms (C #): Convert decimal numbers to binary, octal, and hexadecimal numbers

Source: Internet
Author: User
Tags decimal to binary

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 ();
}

// Convert decimal to binary
Public String dtob (int d)
{
String B = "";
// Determine if the number is smaller than 2, then the output is directly
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;
}

// Convert decimal to octal
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;
}

// Convert decimal to hexadecimal
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); // determines whether the value is greater than 10. If the value is greater than 10, it is converted to ~ F format
D = C;
} While (C> = 16 );
X = Chang (d );
For (Int J = M. Length-1; j> = 0; j --)
{
X + = m [J];
}
}
Return X;
}


// Judge whether the value is 10 ~ The number between 15. If yes, the conversion is performed.
Public String Chang (int d)
{
String x = "";
Switch (d)
{
Case 10:
X = "";
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 button#click (Object sender, eventargs E)
{
Textbox2.text = dtob (convert. toint32 (textbox1.text); // convert 10 to binary
}

Private void button2_click (Object sender, eventargs E)
{
Textbox2.text = dtoo (convert. toint32 (textbox1.text); // 10 to 8 bytes
}

Private void button3_click (Object sender, eventargs E)
{
Textbox2.text = dtox (convert. toint32 (textbox1.text); // convert to 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.