KKboy199 2016-07-22 00:34 采纳率: 100%
浏览 1047
已采纳

C#10進制怎麼與自定義的31進制互相轉換

using System;
using System.Collections.Generic;
using System.Text;

namespace BCPannelDll
{
class AnyRadixConvert1
{
private static char[] UrDigits =
{
'1', '2', '3', '4', '5', '6', '7', '8', '9',
'B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'V',
'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U',
'W', 'X', 'Y', 'Z'
};

    /// <summary>
    /// 将指定基数的数字的 System.String 表示形式转换为等效的 64 位有符号整数。
    /// </summary>
    /// <param name="value">包含数字的 System.String。</param>
    /// <param name="fromBase">value 中数字的基数,它必须是[2,36]</param>
    /// <returns>等效于 value 中的数字的 64 位有符号整数。- 或 - 如果 value 为 null,则为零。</returns>
    private long x2h(string value, int fromBase)
    {
        value = value.Trim();
        if (string.IsNullOrEmpty(value))
        {
            return 0L;
        }

        string UsDigits = new string(UrDigits, 0, fromBase);
        long result = 0;
        value = value.ToUpper();
        for (int i = 0; i < value.Length; i++)
        {
            if (!UsDigits.Contains(value[i].ToString()))
            {
                throw new ArgumentException(string.Format("The argument \"{0}\" is not in {1} system.", value[i], fromBase));
            }
            else
            {
                try
                {
                    result += (long)Math.Pow(fromBase, i) * getcharindex(UrDigits, value[value.Length - i - 1]);//   2
                }
                catch
                {
                    throw new OverflowException("运算溢出.");
                }
            }
        }
        return result;
    }

    private int getcharindex(char[] arr, char value)
    {
        for (int i = 0; i < arr.Length; i++)
        {
            if (arr[i] == value)
            {
                return i;
            }
        }
        return 0;
    }

    //long转化为toBase进制
    private string h2x(long value, int toBase)
    {
        int digitIndex = 0;
        long longPositive = Math.Abs(value);
        int radix = toBase;
        char[] outDigits = new char[63];

        for (digitIndex = 0; digitIndex <= 64; digitIndex++)
        {
            if (longPositive == 0)
            {
                break;
            }
            outDigits[outDigits.Length - digitIndex - 1] = UrDigits[longPositive % radix];
            longPositive /= radix;
        }

        return new string(outDigits, outDigits.Length - digitIndex, digitIndex);
    }

    //任意进制转换,将fromBase进制表示的value转换为toBase进制
    public string X2X(string value, int fromBase, int toBase)
    {
        if (string.IsNullOrEmpty(value.Trim()))
        {
            return string.Empty;
        }

        if (fromBase < 2 || fromBase > 36)
        {
            throw new ArgumentException(String.Format("The fromBase radix \"{0}\" is not in the range 2..36.", fromBase));
        }

        if (toBase < 2 || toBase > 36)
        {
            throw new ArgumentException(String.Format("The toBase radix \"{0}\" is not in the range 2..36.", toBase));
        }

        long m = x2h(value, fromBase);
        string r = h2x(m, toBase);
        return r;
    }


}

}

這段代碼有些問題,傳11111進去轉的時候報錯,求大神幫忙

  • 写回答

1条回答 默认 最新

  • Moluth 2016-07-22 01:00
    关注

    你这里1代表最小值如果传一个这样的数字你觉得合适吗?类似于10进制的00000,直接写0就好了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料