C#:10进制转2进制函数
1 /**//*******************************************
2 * 十进制转二进制函数
3 *******************************************/
4 public string dtb(string buf){
5 int[] temp= new int[20];
6 string binary;
7 int val=0,i=0,j;
8
9 /**//*先将字符转化为十进制数*/
10 try{
11 val = Convert.ToInt32(buf);
12 }catch{
13 val = 0;
14 }
15
16 if(val==0) {
17 return(val.ToString());
18 }
19
20 i=0;
21 while(val!=0) {
22 temp[i++]=val%2;
23 val/=2;
24 }
25
26 binary = "";
27 for(j=0;j<=i-1;j++)
28 binary += (char)(temp[i-j-1]+48);
29
30 return(binary);
31 }
2 * 十进制转二进制函数
3 *******************************************/
4 public string dtb(string buf){
5 int[] temp= new int[20];
6 string binary;
7 int val=0,i=0,j;
8
9 /**//*先将字符转化为十进制数*/
10 try{
11 val = Convert.ToInt32(buf);
12 }catch{
13 val = 0;
14 }
15
16 if(val==0) {
17 return(val.ToString());
18 }
19
20 i=0;
21 while(val!=0) {
22 temp[i++]=val%2;
23 val/=2;
24 }
25
26 binary = "";
27 for(j=0;j<=i-1;j++)
28 binary += (char)(temp[i-j-1]+48);
29
30 return(binary);
31 }