从M进制转换为N进制
2024-10-20 01:29:34
/// <summary>
/// 从M进制转换为N进制
/// </summary>
internal class MBase2NBase
{
/// <summary>
/// M进制值字符串
/// </summary>
private string MValue = ""; /// <summary>
/// 除法商
/// </summary>
private string Quotient = null; /// <summary>
/// M进制
/// </summary>
private int M = ; /// <summary>
/// N进制
/// </summary>
private int N = ; /// <summary>
/// .ctor
/// </summary>
public MBase2NBase()
{ } /// <summary>
/// .ctor
/// </summary>
/// <param name="mValue">M进制值字符串</param>
/// <param name="m">M进制(如10).参数值在2~36的范围内</param>
/// <param name="n">N进制(如8),则意味着从10进制转换成8进制.参数值在2~36的范围内</param>
public MBase2NBase(string mValue, int m, int n)
{
this.M = m;
this.N = n;
this.MValue = mValue;
} /// <summary>
/// 开始转换
/// </summary>
/// <param name="mValue">M进制值字符串</param>
/// <param name="m">M进制(如10).参数值在2~36的范围内</param>
/// <param name="n">N进制(如8),则意味着从10进制转换成8进制.参数值在2~36的范围内</param>
/// <returns>N进制字符串</returns>
public string Transform(string mValue, int m, int n)
{
this.M = m;
this.N = n;
this.MValue = mValue;
return Transform();
} /// <summary>
/// M进制值字符串
/// </summary>
/// <returns>N进制字符串</returns>
public string Transform()
{
if (M < || M > )
{
throw new ArgumentOutOfRangeException("参数值不在2~36的范围内");
} if (N < || N > )
{
throw new ArgumentOutOfRangeException("参数值不在2~36的范围内");
} if (M == N)
{
return MValue;
} String nValue = "";
Quotient = MValue;
while (Quotient.Length > )
{
nValue = Remainder(Quotient) + nValue;
}
return nValue;
} /// <summary>
/// 对给定的M进制字符串对n求余
/// </summary>
/// <param name="mTempValue"></param>
/// <returns></returns>
private String Remainder(String mTempValue)
{
Quotient = "";
int temp = ;
while (mTempValue.Length > )
{
int t = GetIntFromStringFirstChar(mTempValue.Substring(, ));
mTempValue = mTempValue.Substring();
temp = temp*M + t;
Quotient += GetStrFromInt(temp / N);
temp = temp%N;
}
while (Quotient.Length > && Quotient[] == '')
{
Quotient = Quotient.Substring();
}
return GetStrFromInt(temp);
} /// <summary>
/// 字符串转换成Int
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
private int GetIntFromStringFirstChar(String str)
{
return str[] <= '' && str[] >= '' ? str[] - '' : str[] - 'a' + ;
} /// <summary>
/// Int转换为相应进制的字符
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
private String GetStrFromInt(int value)
{
String result = null;
if (value >= && value <= )
result = ((char) ('' + value)).ToString();
else if (value > && value < )
{
result = ((char) ('a' + value - )).ToString();
}
else
{
throw new Exception("未知错误");
} return result;
}
}
最新文章
- JUnit 4 与 TestNG 对比
- hibernate 异常:could not execute statement
- [CareerCup] 18.10 Word Transform 单词转换
- Java 有理数类 分数类 Rational类的设计与实现
- ListIterator-迭代器
- Hadoop on Mac with IntelliJ IDEA - 4 制作jar包
- django 学习-3 模板变量
- ubuntu14.04LTS更新源
- lc面试准备:Power of Two
- java Active Object模式(下)
- Hive 入门(转)
- JAVA基础---面向对象
- bing翻译API调用方法
- Nodejs进阶:服务端字符编解码&;乱码处理
- SystemVerilog语言简介(一)
- node 创建
- ATS6.2安装部署笔记
- JAVA核心技术I---JAVA基础知识(命令行)
- Windows 系统共享文件扫描
- 使用Postman在Chrome下进行rest请求测试