一、将其他进制字符串,转换成十进制
/// <summary>
/// 将指定进制的字符串,转换成十进制
/// </summary>
/// <param name="source">字符串</param>
/// <param name="number">进制数</param>
/// <returns></returns>
public static int ConvertFromN(string source, int fromBase)
{
char[] list = source.ToCharArray();
var r = 0;
for (int i = list.Length - 1; i >= 0; i--)
{
int value = Convert.ToInt32(list[i].ToString());
r += (int)Math.Pow(fromBase, list.Length - i - 1) * value;
}
return r;
}
实例:
//二进制
Console.WriteLine(ConvertFromN("00", 2));//0
Console.WriteLine(ConvertFromN("11", 2));//3
//三进制
Console.WriteLine(ConvertFromN("010", 3));//3
Console.WriteLine(ConvertFromN("110", 3));//12
二、C#幂函数方法,x的y次方
//
// 摘要:
// 返回指定数字的指定次幂。
//
// 参数:
// x:
// 要乘幂的双精度浮点数。
//
// y:
// 指定幂的双精度浮点数。
//
// 返回结果:
// 数字 x 的 y 次幂。
[SecuritySafeCritical]
public static double Pow(double x, double y);
实例:
Console.WriteLine(Math.Pow(2, 0));//1
Console.WriteLine(Math.Pow(2, 1));//2
Console.WriteLine(Math.Pow(2, 2));//4
更多: