/// <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;
}
}

最新文章

  1. JUnit 4 与 TestNG 对比
  2. hibernate 异常:could not execute statement
  3. [CareerCup] 18.10 Word Transform 单词转换
  4. Java 有理数类 分数类 Rational类的设计与实现
  5. ListIterator-迭代器
  6. Hadoop on Mac with IntelliJ IDEA - 4 制作jar包
  7. django 学习-3 模板变量
  8. ubuntu14.04LTS更新源
  9. lc面试准备:Power of Two
  10. java Active Object模式(下)
  11. Hive 入门(转)
  12. JAVA基础---面向对象
  13. bing翻译API调用方法
  14. Nodejs进阶:服务端字符编解码&amp;乱码处理
  15. SystemVerilog语言简介(一)
  16. node 创建
  17. ATS6.2安装部署笔记
  18. JAVA核心技术I---JAVA基础知识(命令行)
  19. Windows 系统共享文件扫描
  20. 使用Postman在Chrome下进行rest请求测试

热门文章

  1. hibernate的子查询
  2. nodejs读文件
  3. 高性能MySQL笔记-第5章Indexing for High Performance-004怎样用索引才高效
  4. linux环境启动django项目
  5. [学习记录]fork压力测试程序
  6. BIO与NIO的方式实现文件拷贝
  7. iOS开发图片与颜色处理工具
  8. Unity苹果(iOS)内购接入(Unity内置IAP)
  9. 深入浅出git
  10. 洛谷P4360 [CEOI2004]锯木厂选址(斜率优化)