[英]Decimal To Binary Conversion
I tried with this code in C# but could not get the desired output and i cudn't find where my mstaking in logic. 我在C#中尝试使用此代码,但无法获得所需的输出,并且我找不到逻辑上的错误。
int rem,n,num=0;
while(n>0)
{
rem=n%2;
num=(num*10)+rem;
n=n/2;
}
Console.WriteLine(num);
But it doesn't give me the right output please tell me how can i accomplish it. 但这不能给我正确的输出,请告诉我我该如何完成。
Output: 输出:
6 after conversion it sould be 110 but its 11 转换后6等于110,但11
You can use method Convert.ToString for that: 您可以为此使用方法Convert.ToString :
string binValue = Convert.ToString(number, 2);
If you nead a leading zeros you can use String PadLeft method: 如果需要前导零,则可以使用String PadLeft方法:
binValue = binValue.PadLeft(10, '0');
您的错误是您将数字以相反的顺序添加到“ num”中。
There's an answer here: Decimal to binary conversion in c # 这里有一个答案: c#中的十进制到二进制转换
Essentially: 实质上:
int value = 8;
string binary = Convert.ToString(value, 2);
Will this solve your problem or do you need to understand why your code wasn't working? 这将解决您的问题,还是您需要了解为什么代码不起作用?
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)
{
}
}
}
Convert Decimal to Binary 将十进制转换为二进制
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 有关更多详细信息,十进制转换为二进制
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;
n
从未设置,因此将始终为零,这意味着将永远不会调用while(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 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");
}
And the result is: 结果是:
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.