2011-05-06 230 views
-1

我用C#中的這段代碼嘗試過,但無法獲得所需的輸出結果,並且找不到我在邏輯中進行mstaking的地方。十進制到二進制轉換

int rem,n,num=0; 
while(n>0) 
{ 
    rem=n%2; 
    num=(num*10)+rem; 
    n=n/2; 
} 
Console.WriteLine(num); 

但它並沒有給我正確的輸出,請告訴我我該如何實現它。
輸出:
6轉換後,前人的精力是110,但它的11

+3

你不提對輸入是什麼東西,以及輸出是什麼,以及你的意圖該算法是。您應詳細說明所有這些情況,以便其他人可以幫助您提供答案。 – casperOne 2011-05-06 12:30:41

+0

就像我告訴我的測試人員:預期的,實際的,重現的步驟。你只有其中一個,開發人員總是需要這三個。 – 2011-05-06 12:32:34

+0

@Redx我的意思是我也可以接受C++的答案,因爲我也使用它。 – avirk 2011-05-06 12:33:50

回答

8

您可以使用方法Convert.ToString爲:

string binValue = Convert.ToString(number, 2); 

如果NEAD一個前導零,您可以使用String PadLeft方法:

binValue = binValue.PadLeft(10, '0'); 
0

n從未設置,因此將永遠是零,這意味着你的while(n>0)循環將永遠不會被調用。

+1

n從來沒有設置,因此程序不會編譯'使用未分配的變量'錯誤。 ;) – Dmitry 2011-05-06 12:34:47

+0

德米特里 - 我認爲你的意思是警告,因爲它被賦值爲0. – 2011-05-06 12:35:32

+1

不,我的意思是'錯誤'。 (剛剛用VS2010測試過,程序不能編譯)。也許有一些設置將它視爲警告,但默認情況下是錯誤的。 – Dmitry 2011-05-06 12:40:47

0
From Decimal to Binary... 


using System; 

class Program{ 

    static void Main(string[] args){ 

     try{ 

    int i = (int)Convert.ToInt64(args[0]); 
     Console.WriteLine("\n{0} converted to Binary is {1}\n",i,ToBinary(i)); 

     }catch(Exception e){ 

     Console.WriteLine("\n{0}\n",e.Message); 

     } 

    }//end Main 


     public static string ToBinary(Int64 Decimal) 
     { 
      // Declare a few variables we're going to need 
      Int64 BinaryHolder; 
      char[] BinaryArray; 
      string BinaryResult = ""; 

      while (Decimal > 0) 
      { 
       BinaryHolder = Decimal % 2; 
       BinaryResult += BinaryHolder; 
       Decimal = Decimal/2; 
      } 

      // The algoritm gives us the binary number in reverse order (mirrored) 
      // We store it in an array so that we can reverse it back to normal 
      BinaryArray = BinaryResult.ToCharArray(); 
      Array.Reverse(BinaryArray); 
      BinaryResult = new string(BinaryArray); 

      return BinaryResult; 
     } 


}//end class Program 



From Binary to Decimal... 


using System; 

class Program{ 

    static void Main(string[] args){ 

     try{ 

     int i = ToDecimal(args[0]); 
     Console.WriteLine("\n{0} converted to Decimal is {1}",args[0],i); 

     }catch(Exception e){ 

     Console.WriteLine("\n{0}\n",e.Message); 

     } 

    }//end Main 


       public static int ToDecimal(string bin) 
     { 
        long l = Convert.ToInt64(bin,2); 
        int i = (int)l; 
        return i; 
     } 


}//end class Program 

取自this的代碼片段。

0
string binary = ""; 
while (decimalNum != 0) { 
    int nextDigit = decimalNum & 0x01; 
    binary = nextDigit + binary; 
    decimalNum = decimalNum >> 1; 
} 

Console.WriteLine(binary); 
0
int n = 100; 
for (int i = sizeof(n) * 8 - 1; i >= 0; --i) { 
    n & (1 << i) ? printf("1") : printf("0"); 
} 

,其結果是:

00000000000000000000000001100100 
0
int number = value; 
int rem = 0; 
int round = 0; 
int result = 0; 

while(number > 1) 
{ 
    rem = number % 2; 
    result = result + (rem * (round^10)); 
    number = number/2; 

    round ++; 
}  

result = result + (number * (round^10)); 



Console.WriteLine(result); 
+0

它給出的錯誤是「使用未分配的局部變量舍入」。 – avirk 2011-05-06 13:10:32

+0

@avrik:現在檢查它.. – 2011-05-06 13:11:50

3

您的錯誤是,您將以相反的順序將數字添加到「num」。

3

有一個答案在這裏:Decimal to binary conversion in C#

本質:

int value = 8; 
string binary = Convert.ToString(value, 2); 

這是否會解決您的問題,或者你需要理解爲什麼你的代碼不能正常工作?

0
using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 
     private void Form1_Load(object sender, EventArgs e) 
     { 

     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      if (radioButton1.Checked == true) 
       prime_not(); 
      else 
       binary(); 
     } 



     private void binary() 
     { 
      label1.Text = Convert.ToString(Convert.ToInt64(textBox1.Text), 2); 
     } 

     private void prime_not() 
     { 
      if (Convert.ToInt16(textBox1.Text) % 2 == 0) 
       label1.Text= "Not Prime"; 
      else 
       label1.Text = "Prime"; 
     } 

     private void label1_Click(object sender, EventArgs e) 
     { 

     } 

     private void label1_Click_1(object sender, EventArgs e) 
     { 

     } 

    } 
} 
0

十進制轉換爲二進制

int len = 8; 

public string DeicmalToBin(int value, int len) 
{ 
     try 
     { 
      return (len > 1 ? DeicmalToBin(value >> 1, len - 1) : null) + "01"[value & 1]; 
     } 
     catch(Exception ex){ 
      Console.Write(ex.Message); 
     } 
     return ""; 
    } 

For more details Decimal to binary conversion