今天碰到了十进制转换二进制的问题,自己写了一个函数后,后来又找到了系统函数可以调用,倒很省事。我写的函数是:
public string convert(int _a)
{
string _b = "";
if (_a==0)
{
_b="0";
}
if(_a!=0)
{
while(_a/2!=0)
{
if(_a%2==0)
_b = "0" + _b;
else
_b = "1" + _b;
_a = _a/2;
}
_b = "1" + _b;
}
return _b;
}
相应系统函数是:ToString(int value, System.IFormatProvider provider)
另外系统还有二进制转换十进制函数:ToInt16(string value, System.IFormatProvider provider)
public string convert(int _a)
{
string _b = "";
if (_a==0)
{
_b="0";
}
if(_a!=0)
{
while(_a/2!=0)
{
if(_a%2==0)
_b = "0" + _b;
else
_b = "1" + _b;
_a = _a/2;
}
_b = "1" + _b;
}
return _b;
}
相应系统函数是:ToString(int value, System.IFormatProvider provider)
另外系统还有二进制转换十进制函数:ToInt16(string value, System.IFormatProvider provider)