//转换为十进制
Convert.ToInt32("值字符串", 字符串的进制);
// 十进制转为其他进制
Convert.ToString(十进制值, 要转换的进制);
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
using System;
using System.Linq;
using System.Net.Security;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
const float PI = 3.14f;
Console.WriteLine(ConvertToOthers(125.1527, 2,8));
Console.WriteLine(ConvertToOthers(125.1527, 8,5));
Console.WriteLine(ConvertToTen(11010.110.ToString(),2));
Console.WriteLine(ConvertToTen(322.52.ToString(),8));
Console.WriteLine(ConvertToOthers(86541.5984, 16,5));
Console.ReadLine();
}
//转换为十进制
static double ConvertToTen(string number,int level)
{
string intPart = number.Split(".")[0];
string decimalPart = number.Split(".")[1];
double intPartResult = ConvertIntToTen(intPart, level);
double decimalPartResult = ConvertDecimalPartToTen(decimalPart, level);
return intPartResult + decimalPartResult;
}
//十进制转换为二进制或八进制
static double ConvertToOthers(double n,int level,int decimalSize)
{
int intPart = (int) n;
double decimalPart = n - intPart;
double intResult = Convert.ToDouble(ConvertTenToOthers(intPart.ToString(), level));
double decimalResult = ConvertDecimalPartToOthers(decimalPart, level, decimalSize);
return intResult+ decimalResult;
}
//十进制转换为十六进制
static string ConvertsixToTen(double n, int level, int decimalSize)
{
int intPart = (int)n;
double decimalPart = n - intPart;
return ConvertTenToOthers(intPart.ToString(), level) + ConvertDecimalPartToOthers(decimalPart, level, decimalSize).ToString().Split(".")[1];
}
//十进制整数转换为其他进制
static string ConvertTenToOthers(String n,int level)
{
string result = string.Empty;
int number;
bool b= Int32.TryParse(n,out number);
if (!b)
{
return "";
}
int i = 0;
int m = 0;
while (number > level-1)
{
i = number / level;
m = number % level;
if (level==16 && m>9)
{
result = getCharFromHexNumber(m) + result;
}
else
{
result = m + result;
}
number = i;
}
if (i>0)
{
if (level == 16 && i > 9)
{
result = getCharFromHexNumber(i) + result;
}
else
{
result = i + result;
}
}
return result;
}
static char getCharFromHexNumber(int m)
{
char result=default(char);
switch (m)
{
case 10:
result = 'A';
break;
case 11:
result = 'B' ;
break;
case 12:
result = 'C' ;
break;
case 13:
result = 'D';
break;
case 14:
result = 'E';
break;
case 15:
result = 'F';
break;
}
return result;
}
static int getHexNumberFromChar(char m)
{
int iNumber = default(int);
switch (m)
{
case 'A':
iNumber = 10;
break;
case 'B':
iNumber = 11;
break;
case 'C':
iNumber = 12;
break;
case 'D':
iNumber = 13;
break;
case 'E':
iNumber = 14;
break;
case 'F':
iNumber = 15;
break;
default:
Int32.TryParse(m.ToString(), out iNumber);
break;
}
return iNumber;
}
//整数转为十进制
static double ConvertIntToTen(string number,int leverl)
{
double result = default(double);
for (int i= 0; i<number.Length;i++)
{
int iNumber ;
if (leverl==16)
{
iNumber=getHexNumberFromChar(number.ToUpper()[i]);
}
else
{
bool b = Int32.TryParse(number[i].ToString(), out iNumber);
if (!b)
{
break;
}
}
result =result+ Math.Pow(leverl,number.Length-1-i) * iNumber;
}
return result;
}
//小数部分转换为二进制或八进制
static double ConvertDecimalPartToOthers(double n, int level,int decimalSize)
{
string result = "0.";
int i = 0;
while (i<decimalSize+1)
{
double temp=n * level;
int intPart = (int) temp;
double decimalPart = temp - intPart;
if (level==16 && intPart>9)
{
result += getCharFromHexNumber(intPart);
}
else
{
result += intPart.ToString();
}
n = decimalPart;
i++;
}
return Convert.ToDouble(result);
}
//小数部分转为十进制
static double ConvertDecimalPartToTen(string number, int leverl)
{
double result = default(double);
for (int i = 0; i < number.Length; i++)
{
int iNumber;
if (leverl == 16)
{
iNumber = getHexNumberFromChar(number.ToUpper()[i]);
}
else
{
bool b = Int32.TryParse(number[i].ToString(), out iNumber);
if (!b)
{
break;
}
}
result = result + Math.Pow(leverl, -i-1) * iNumber;
}
return (double)Math.Round(result, 5);
}
}
}