各进制数间转换

#region 各进制数间转换
/// <summary>
/// 实现各进制数间的转换。ConvertBase("15",10,16)表示将十进制数15转换为16进制的数。
/// </summary>
/// <param name="value">要转换的值,即原值</param>
/// <param name="from">原值的进制,只能是2,8,10,16四个值。</param>
/// <param name="to">要转换到的目标进制,只能是2,8,10,16四个值。</param>
public static string ConvertBase(string value, int from, int to)
{
if (!isBaseNumber(from))
throw new ArgumentException("参数from只能是2,8,10,16四个值。");

if (!isBaseNumber(to))
throw new ArgumentException("参数to只能是2,8,10,16四个值。");

int intValue = Convert.ToInt32(value, from); //先转成10进制
string result = Convert.ToString(intValue, to); //再转成目标进制
if (to == 2)
{
int resultLength = result.Length; //获取二进制的长度
switch (resultLength)
{
case 7:
result = "0" + result;
break;
case 6:
result = "00" + result;
break;
case 5:
result = "000" + result;
break;
case 4:
result = "0000" + result;
break;
case 3:
result = "00000" + result;
break;
}
}
return result;
}

/// <summary>
/// 判断是否是 2 8 10 16
/// </summary>
/// <param name="baseNumber"></param>
/// <returns></returns>
private static bool isBaseNumber(int baseNumber)
{
if (baseNumber == 2 || baseNumber == 8 || baseNumber == 10 || baseNumber == 16)
return true;
return false;
}

#endregion

posted on 2016-06-20 13:46  張暁磊  阅读(200)  评论(0编辑  收藏  举报