我尝试在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
答案 0 :(得分:8)
您可以使用方法Convert.ToString:
string binValue = Convert.ToString(number, 2);
如果您需要前导零,可以使用String PadLeft方法:
binValue = binValue.PadLeft(10, '0');
答案 1 :(得分:3)
您的错误是您以相反的顺序将数字添加到“数字”。
答案 2 :(得分:3)
这里有一个答案:Decimal to binary conversion in c #
本质:
int value = 8;
string binary = Convert.ToString(value, 2);
这会解决您的问题,还是您需要了解代码无效的原因?
答案 3 :(得分:0)
n
永远不会设置,因此始终为零,这意味着永远不会调用while(n>0)
循环。
答案 4 :(得分: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的代码段。
答案 5 :(得分:0)
string binary = "";
while (decimalNum != 0) {
int nextDigit = decimalNum & 0x01;
binary = nextDigit + binary;
decimalNum = decimalNum >> 1;
}
Console.WriteLine(binary);
答案 6 :(得分:0)
int n = 100;
for (int i = sizeof(n) * 8 - 1; i >= 0; --i) {
n & (1 << i) ? printf("1") : printf("0");
}
结果是:
00000000000000000000000001100100
答案 7 :(得分: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);
答案 8 :(得分: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)
{
}
}
}
答案 9 :(得分: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 "";
}
答案 10 :(得分: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;