十进制到二进制转换

问题描述 投票:-1回答:11

我尝试在C#中使用此代码,但无法获得所需的输出,我无法找到我在逻辑中的mstaking。

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

但它没有给我正确的输出请告诉我如何才能实现它。 输出: 转换后,它应该是110但是它的11

c#
11个回答
8
投票

您可以使用方法Convert.ToString

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

如果你需要一个前导零,你可以使用String PadLeft方法:

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

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


0
投票
 int num = Convert.ToInt32(textBox1.Text);
 int rem = 0;
 string res = "";

 do
 {
     rem = num % 2;
     num /= 2;
     res=rem.ToString()+res;               
 } while (num > 0);

 textBox1.Text=res;

3
投票

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


3
投票

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

实质上:

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

这会解决您的问题,还是您需要了解为什么您的代码不起作用?


0
投票

n永远不会被设置,因此总是为零,这意味着你的while(n>0)循环永远不会被调用。


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

        }

    }
}
© www.soinside.com 2019 - 2024. All rights reserved.