2015-09-13 83 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();