//十进制转二制
public string Dto2(int d)
{
string b = "";
//判断该数如果小于2,则直接输出
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;
}
效果如下