【问题标题】:C# Converting From Base to Decimal went CrazyC# 从基数转换为十进制太疯狂了
【发布时间】:2017-08-12 21:54:35
【问题描述】:

所以我刚刚想到将任何基数转换为十进制(以 10 为基数):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SoloLearn
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("What is the number you want to convert?");
            string num = Console.ReadLine();
            Console.WriteLine("In what base is this number?");
            int mathBase = int.Parse(Console.ReadLine());
            double output = 0;
            int j = 0;
            char[] nums = num.ToCharArray();
            for(int i=num.Length - 1; i>=0; i--) {
               output = output + Math.Pow(mathBase,i) * nums[j] * 1;
               Console.WriteLine("i: " + i +", j:" + j + ", nums[j]: " + nums[j] + ", output: " + output + ", mathBase: " + mathBase + ", " + Math.Pow(mathBase,i) + ".");
               j++; 
            }
            Console.WriteLine("The number " + num + " in base 10 (Decimal) is " + output + ".");
            Console.ReadLine();
        }
    }
}

所以我从二进制 (num = 100, mathBase = 2) 开始,但答案变得疯狂。这就是为什么我添加了这段代码来看看到底发生了什么:

Console.WriteLine("i: " + i +", j:" + j + ", nums[j]: " + nums[j] + ", output: " + output + ", mathBase: " + mathBase + ", " + Math.Pow(mathBase,i) + "."); 
j++;  

好吧,所有变量都是正确的:

所以是的,我真的不知道会发生什么,因为所有的计算似乎都是正确的(Math.Pow(mathBase,i) = 2^2 = 4,如图所示,但是,4 * nums[j] = 4 * 1 = 196?如果有人知道到底发生了什么,请告诉我!

【问题讨论】:

    标签: c# binary type-conversion decimal base


    【解决方案1】:

    您的问题是numschar[]。因此nums[j] 是一个字符。现在您看到的问题是 char 可以隐式转换为数字,但不是您想要的方式 - 它使用它的 ascii 值,因此 1 是 ascii 值 49,0 是 ascii 值 48。那就是当数学都错了。

    您需要做的是将输入字符串转换为整数数组,而不是字符数组。一些可能执行此操作的示例代码是:

    int[] nums = num.Select(x=>x-'0').ToArray();
    

    它的作用是利用字符 0-9 是 ascii 48-57 的事实,如果你从每个字符中去掉 ascii 0 (48),你会得到 0-9。它还利用了字符串也是 IEnumerable<char> 的事实,因此可以轻松地进行 LINQed。

    通过此修改,您的程序应该可以按预期运行。

    【讨论】:

      【解决方案2】:

      这里的 num[j] 是一个字符,所以你应该在任何计算之前将它转换为 int。

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.Threading.Tasks;
      
      namespace SoloLearn
      {
          class Program
          {
              static void Main(string[] args)
              {
                  Console.WriteLine("What is the number you want to convert?");
                  string num = Console.ReadLine();
                  Console.WriteLine("In what base is this number?");
                  int mathBase = int.Parse(Console.ReadLine());
                  double output = 0;
                  int j = 0;
                  char[] nums = num.ToCharArray();
                  for(int i=num.Length - 1; i>=0; i--) {
                     output = output + Math.Pow(mathBase,i) * Int32.Parse(nums[j].ToString()) * 1;
                     Console.WriteLine("i: " + i +", j:" + j + ", nums[j]: " + nums[j] + ", output: " + output + ", mathBase: " + mathBase + ", " + Math.Pow(mathBase,i) + ".");
                     j++; 
                  }
                  Console.WriteLine("The number " + num + " in base 10 (Decimal) is " + output + ".");
                  Console.ReadLine();
              }
          }
      }
      

      【讨论】:

        【解决方案3】:

        任意基数转换时,您必须处理至少三个范围

           '0'..'9' corresponds to  0..9
           'a'..'z' corresponds to 10..36
           'A'..'Z' corresponds to 10..36
        

        所以你必须在你的代码中插入这个:

           for (int i=num.Length - 1; i>=0; i--) {
             // actual char, say 'C'
             int c = nums[j];
             // corresponding integer value, 12
             int v = 0;
        
             if (c >= '0' && c <= '9')
               v = c - '0';
             else if (c >= 'a' && c <= 'z')
               v = c - 'a' + 10; 
             else if (c >= 'A' && c <= 'Z')
               v = c - 'A' + 10; 
        
             // please, notice not nums[j] but the value it corresponds to - v
             output = output + Math.Pow(mathBase, i) * v;
             ...
           }
        

        附:在 Linq 的帮助下,您可以简洁地说:

          string num = "abC02";
          int mathBase = 16;
        
          int result = num
            .Select(c =>
                 c >= '0' && c <= '9' ? c - '0'
               : c >= 'a' && c <= 'z' ? c - 'a' + 10
               : c >= 'A' && c <= 'Z' ? c - 'A' + 10
               : 0)
            .Aggregate((s, a) => s * mathBase + a);
        
          // 703490
          Console.Write(result);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-11-18
          • 2011-12-22
          • 2018-10-14
          • 1970-01-01
          • 1970-01-01
          • 2014-12-31
          • 2017-07-31
          • 2013-07-20
          相关资源
          最近更新 更多