Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 十进制到二进制转换_C# - Fatal编程技术网

C# 十进制到二进制转换

C# 十进制到二进制转换,c#,C#,我在C#中尝试了这段代码,但无法获得所需的输出,而且我也找不到我的MST在逻辑中的位置 int rem,n,num=0; while(n>0) { rem=n%2; num=(num*10)+rem; n=n/2; } Console.WriteLine(num); 但是它没有给我正确的输出。请告诉我如何完成它。 输出: 6转换后,它应该是110,但它的11从来没有设置过,因此将始终为零,这意味着您的,而(n>0)循环将永远不会被调用

我在C#中尝试了这段代码,但无法获得所需的输出,而且我也找不到我的MST在逻辑中的位置

int rem,n,num=0; 
while(n>0)  
{ 
    rem=n%2;  
    num=(num*10)+rem;  
    n=n/2;  
}  
Console.WriteLine(num);  
但是它没有给我正确的输出。请告诉我如何完成它。
输出:

6转换后,它应该是110,但它的11从来没有设置过,因此将始终为零,这意味着您的
,而(n>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
代码段取自。

您可以使用以下方法:

string binValue = Convert.ToString(number, 2);
如果需要一个前导零,可以使用以下方法:


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

这里有一个答案:

基本上:

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 "";
    }

您没有提到输入是什么,输出是什么,以及算法的意图是什么。您应该详细说明所有这些事情,以便其他人能够帮助您提供答案。就像我告诉我的测试人员:预期的、实际的、复制步骤。你只有其中一个,开发者总是需要三。@ Rexx,我也可以接受C++中的答案,因为我也用它。@ AvRik:你有一个逻辑问题。N永远不会被设置,因此程序不会编译“使用未赋值变量”。Dmitry-我想你指的是警告,因为它被赋值为0。不,我指的是“错误”。(刚刚用VS2010测试过。程序不编译。)可能有一些设置将其视为警告,但默认情况下这是一个错误。@Ramhound
int-rem,n,num=0仅设置num.rem和n保持未分配状态。它们不默认为0。只有字段接受默认值,而不是局部变量。它给出的错误是“使用未分配的局部变量舍入”。感谢您提供此代码段,它可能会提供一些有限的、即时的帮助。通过展示为什么这是一个很好的问题解决方案,A将极大地提高它的长期价值,并将使它对未来有其他类似问题的读者更有用。请在您的答案中添加一些解释,包括您所做的假设。不鼓励只使用代码的答案。请点击并添加一些文字,总结您的代码如何解决问题,或者解释您的答案与之前的答案有何不同。
string binValue = Convert.ToString(number, 2);
binValue = binValue.PadLeft(10, '0');
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 "";
    }
 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;