天天看点

java二进制转十六进制算法_java_十进制数转换为二进制,八进制,十六进制数的算法...

java Ê®½øÖÆÊýת»»Îª¶þ½øÖÆ,°Ë½øÖÆ,Ê®Áù½øÖÆÊýµÄË㕨

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 classForm1 : Form

{publicForm1()

{

InitializeComponent();

}//Ê®½øÖÆת¶þÖÆ

public string DtoB(intd)

{

string b= "";//ÅжϸÃÊýÈç¹ûСÓÚ2£¬ÔòÖ±½ÓÊä³ö

if (d < 2)

{

b=d.ToString();

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

}

}returnb;

}//Ê®½øÖÆת°Ë½øÖÆ

public string DtoO(intd)

{

string o= "";if (d < 8)

{

o=d.ToString();

}else{intc;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];

}

}returno;

}//Ê®½øÖÆתʮÁù½øÖÆ

public string DtoX(intd)

{

string x= "";if (d < 16)

{

x=chang(d);

}else{intc;int s = 0;int n =d;int temp =d;while (n >= 16)

{

s++;

n= n / 16;

}

string [] m= newstring[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];

}

}returnx;

}//ÅжÏÊÇ•ñΪ10~15Ö®¼äµÄÊý£¬Èç¹ûÊÇÔò½øÐÐת»»

public string chang(intd)

{

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;

}returnx;

}private voidbutton1_Click(object sender, EventArgs e)

{

textBox2.Text= DtoB(Convert.ToInt32(textBox1.Text));//ʮת¶þ½øÖÆ

}private voidbutton2_Click(object sender, EventArgs e)

{

textBox2.Text= DtoO(Convert.ToInt32(textBox1.Text));//ʮת°Ë½øÖÆ

}private voidbutton3_Click(object sender, EventArgs e)

{

textBox2.Text= DtoX(Convert.ToInt32(textBox1.Text));//ʮתʮÁù½øÖÆ

}

}

}