2015-09-13 43 views
1

我是一名C#新手,学习如何使用数组。我编写了一个小型控制台应用程序,可将二进制数转换为十进制数;然而,我使用的sytax似乎导致应用程序 - 在某些时候 - 使用unicode指定整数而不是整数本身的真值,因此1变为49,而0变为48.将字符串转换为整型数组C#

如何以不同的方式编写应用程序以避免这种情况?由于

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

namespace Sandbox 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      Console.WriteLine("Key in binary number and press Enter to calculate decimal equivalent"); 
      string inputString = Console.ReadLine(); 

      ////This is supposed to change the user input into character array - possible issue here 
      char[] digitalState = inputString.ToArray(); 


      int exponent = 0; 
      int numberBase = 2; 

      int digitIndex = inputString.Length - 1; 
      int decimalValue = 0; 
      int intermediateValue = 0; 

      //Calculates the decimal value of each binary digit by raising two to the power of the position of the digit. The result is then multiplied by the binary digit (i.e. 1 or 0, the "digitalState") to determine whether the result should be accumulated into the final result for the binary number as a whole ("decimalValue"). 

      while (digitIndex > 0 || digitIndex == 0) 
      { 

       intermediateValue = (int)Math.Pow(numberBase, exponent) * digitalState[digitIndex]; //The calculation here gives the wrong result, possibly because of the unicode designation vs. true value issue 
       decimalValue = decimalValue + intermediateValue; 


       digitIndex--; 
       exponent++; 


      } 

      Console.WriteLine("The decimal equivalent of {0} is {1}", inputString, intermediateValue); 
      Console.ReadLine(); 


     } 
    } 
} 
+5

我知道它并没有真正回答你的问题,但所有的代码可以减少到'Convert.ToInt32(inputString,2)' - 参见[这个问题](http://stackoverflow.com/questions/9149728/convert-binary-string-int-integer)。 –

+0

不用担心。我正在努力解决数组和循环结构的语法问题。因此代码非常笨重。你的方法仍然值得了解 - 谢谢。 – Zengetsu

+0

具有讽刺意味的是,这个问题正确地引用了Unicode,而几个答案错误地引用了ASCII。 –

回答

0

有两个错误:你错过了“-48”并写了中间代替结果(最后一行)。不知道如何unline的代码块某些部分;)

intermediateValue = (int)Math.Pow(numberBase, exponent) * (digitalState[digitIndex]-48; 
//The calculation here gives the wrong result, 
//possibly because of the unicode designation vs. true value issue 
decimalValue += intermediateValue; 
(.....) 
Console.WriteLine("The decimal equivalent of {0} is {1}", inputString, decimalValue); 
+0

工作过,谢谢!完全错过了第二个错误 – Zengetsu

1

只需使用下面的代码

for (int i = 0; i < digitalState.Length; i++) 
{ 
    digitalState[i] = (char)(digitalState[i] - 48); 
} 

char[] digitalState = inputString.ToArray(); 

注意,一个字符的值,例如 '1' 是从它代表着不同。正如你已经注意到'1'等于ASCII码49.当你从它的值(49)中减去48时,它变成1.

0

@CharlesMager说了这一切。但是,我认为这是一项家庭作业。正如你所说乘以ASCII值是错误的。所以只需从ASCII值中减去'0'(十进制值48)即可。

intermediateValue = (int)Math.Pow(numberBase, exponent) 
    * ((int)digitalState[digitIndex] - 48); 

你的代码很丑,没有必要从字符串中倒退。同样使用Math.Power效率不高,shifting (<<)对于二进制功能是等效的。

long v = 0; 
foreach (var c in inputString) 
{ 
    v = (v << 1) + ((int)c - 48); 
} 
Console.WriteLine("The decimal equivalent of {0} is {1}", inputString, v); 
Console.ReadLine();