十进制到二进制转换

问题描述:

我用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

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

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

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

binValue = binValue.PadLeft(10, '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

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的代码片段。

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

Console.WriteLine(binary); 

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

,其结果是:

00000000000000000000000001100100 

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

您的错误是,您将以相反的顺序将数字添加到“num”。

有一个答案在这里:Decimal to binary conversion in C#

本质:

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

这是否会解决您的问题,或者你需要理解为什么你的代码不能正常工作?

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) 
     { 

     } 

    } 
} 

十进制转换为二进制

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