如何将整数转换为二进制表示?
我正在使用下面的代码:
String input = "8";
String output = Convert.ToInt32(input, 2).ToString();
但是它抛出了一个异常:
找不到任何可解析的数字
如何将整数转换为二进制表示?
我正在使用下面的代码:
String input = "8";
String output = Convert.ToInt32(input, 2).ToString();
但是它抛出了一个异常:
找不到任何可解析的数字
当前回答
如果你想要一个简洁的函数,你可以从你的主方法调用,在你的类中,这可能是有帮助的。如果你需要一个数字而不是字符串,你可能仍然需要调用int.Parse(toBinary(someint)),但我发现这个方法工作得很好。此外,如果您愿意,还可以调整为使用for循环而不是do-while循环。
public static string toBinary(int base10)
{
string binary = "";
do {
binary = (base10 % 2) + binary;
base10 /= 2;
}
while (base10 > 0);
return binary;
}
toBinary(10)返回字符串“1010”。
其他回答
非常简单,没有额外的代码,只是输入,转换和输出。
using System;
namespace _01.Decimal_to_Binary
{
class DecimalToBinary
{
static void Main(string[] args)
{
Console.Write("Decimal: ");
int decimalNumber = int.Parse(Console.ReadLine());
int remainder;
string result = string.Empty;
while (decimalNumber > 0)
{
remainder = decimalNumber % 2;
decimalNumber /= 2;
result = remainder.ToString() + result;
}
Console.WriteLine("Binary: {0}",result);
}
}
}
转换。ToInt32(string, base)不会将基数转换为基数。它假设字符串包含一个以指定基数为底的有效数字,并转换为以10为基数。
所以你会得到一个错误,因为“8”不是一个以2为基数的有效数字。
String str = "1111";
String Ans = Convert.ToInt32(str, 2).ToString();
将显示15(1111以2为底= 15以10为底)
String str = "f000";
String Ans = Convert.ToInt32(str, 16).ToString();
将显示61440。
我在一个编码挑战中遇到了这个问题,你必须将32位十进制转换为二进制,并找到子字符串的可能组合。
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Numerics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp2
{
class Program
{
public static void Main()
{
int numberofinputs = int.Parse(Console.ReadLine());
List<BigInteger> inputdecimal = new List<BigInteger>();
List<string> outputBinary = new List<string>();
for (int i = 0; i < numberofinputs; i++)
{
inputdecimal.Add(BigInteger.Parse(Console.ReadLine(), CultureInfo.InvariantCulture));
}
//processing begins
foreach (var n in inputdecimal)
{
string binary = (binaryconveter(n));
subString(binary, binary.Length);
}
foreach (var item in outputBinary)
{
Console.WriteLine(item);
}
string binaryconveter(BigInteger n)
{
int i;
StringBuilder output = new StringBuilder();
for (i = 0; n > 0; i++)
{
output = output.Append(n % 2);
n = n / 2;
}
return output.ToString();
}
void subString(string str, int n)
{
int zeroodds = 0;
int oneodds = 0;
for (int len = 1; len <= n; len++)
{
for (int i = 0; i <= n - len; i++)
{
int j = i + len - 1;
string substring = "";
for (int k = i; k <= j; k++)
{
substring = String.Concat(substring, str[k]);
}
var resultofstringanalysis = stringanalysis(substring);
if (resultofstringanalysis.Equals("both are odd"))
{
++zeroodds;
++oneodds;
}
else if (resultofstringanalysis.Equals("zeroes are odd"))
{
++zeroodds;
}
else if (resultofstringanalysis.Equals("ones are odd"))
{
++oneodds;
}
}
}
string outputtest = String.Concat(zeroodds.ToString(), ' ', oneodds.ToString());
outputBinary.Add(outputtest);
}
string stringanalysis(string str)
{
int n = str.Length;
int nofZeros = 0;
int nofOnes = 0;
for (int i = 0; i < n; i++)
{
if (str[i] == '0')
{
++nofZeros;
}
if (str[i] == '1')
{
++nofOnes;
}
}
if ((nofZeros != 0 && nofZeros % 2 != 0) && (nofOnes != 0 && nofOnes % 2 != 0))
{
return "both are odd";
}
else if (nofZeros != 0 && nofZeros % 2 != 0)
{
return "zeroes are odd";
}
else if (nofOnes != 0 && nofOnes % 2 != 0)
{
return "ones are odd";
}
else
{
return "nothing";
}
}
Console.ReadKey();
}
}
}
您的示例中有一个表示为字符串的整数。假设你的整数实际上是一个整数,你想把这个整数转换成二进制字符串。
int value = 8;
string binary = Convert.ToString(value, 2);
返回1000。
http://zamirsblog.blogspot.com/2011/10/convert-decimal-to-binary-in-c.html
public string DecimalToBinary(string data)
{
string result = string.Empty;
int rem = 0;
try
{
if (!IsNumeric(data))
error = "Invalid Value - This is not a numeric value";
else
{
int num = int.Parse(data);
while (num > 0)
{
rem = num % 2;
num = num / 2;
result = rem.ToString() + result;
}
}
}
catch (Exception ex)
{
error = ex.Message;
}
return result;
}