十进制到二进制转换在C#

如何将一个十进制转换成二进制在C#

现在我使用像:

String str="8"; String Ans= Convert.ToInt32(str,2).ToString(); 

但它抛出了一些例外。

如何做到这一点?

你的例子有一个整数expression为一个string。 假设您的整数实际上是一个整数,并且您希望取整数并将其转换为二进制string。

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

其中返回1000。

**从任何经典的基地转换为任何基地在C#**

 String number = "100"; int fromBase = 16; int toBase = 10; String result = Convert.ToString(Convert.ToInt32(number, fromBase), toBase); // result == "256" 

支持的基数是2,8,10和16

非常简单,没有额外的代码,只需input,转换和输出。

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

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

Convert.ToInt32(string, base)不做基地转换到您的基地。 它假定该string在指定的基数中包含有效数字,并将其转换为基数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

 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); } } 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; } BinaryArray = BinaryResult.ToCharArray(); Array.Reverse(BinaryArray); BinaryResult = new string(BinaryArray); return BinaryResult; } 

我知道这个答案看起来类似于大部分已经在这里的答案,但我注意到几乎没有人使用for循环。 这个代码可以工作,并且可以被认为是简单的,从某种意义上说,它可以在没有任何特殊function的情况下工作,比如带参数的ToString(),也不会太长。 也许有些人喜欢for循环而不是while循环,这可能适合他们。

 public static string ByteConvert (int num) { int[] p = new int[8]; string pa = ""; for (int ii = 0; ii<= 7;ii = ii +1) { p[7-ii] = num%2; num = num/2; } for (int ii = 0;ii <= 7; ii = ii + 1) { pa += p[ii].ToString(); } return pa; } 
 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 

原始的方式:

 public string ToBinary(int n) { if (n < 2) return n.ToString(); var divisor = n / 2; var remainder = n % 2; return ToBinary(divisor) + remainder; } 
  // I use this function public static string ToBinary(long number) { string digit = Convert.ToString(number % 2); if (number >= 2) { long remaining = number / 2; string remainingString = ToBinary(remaining); return remainingString + digit; } return digit; } 
 class Program { static void Main(string[] args) { var @decimal = 42; var binaryVal = ToBinary(@decimal, 2); var binary = "101010"; var decimalVal = ToDecimal(binary, 2); Console.WriteLine("Binary value of decimal {0} is '{1}'", @decimal, binaryVal); Console.WriteLine("Decimal value of binary '{0}' is {1}", binary, decimalVal); Console.WriteLine(); @decimal = 6; binaryVal = ToBinary(@decimal, 3); binary = "20"; decimalVal = ToDecimal(binary, 3); Console.WriteLine("Base3 value of decimal {0} is '{1}'", @decimal, binaryVal); Console.WriteLine("Decimal value of base3 '{0}' is {1}", binary, decimalVal); Console.WriteLine(); @decimal = 47; binaryVal = ToBinary(@decimal, 4); binary = "233"; decimalVal = ToDecimal(binary, 4); Console.WriteLine("Base4 value of decimal {0} is '{1}'", @decimal, binaryVal); Console.WriteLine("Decimal value of base4 '{0}' is {1}", binary, decimalVal); Console.WriteLine(); @decimal = 99; binaryVal = ToBinary(@decimal, 5); binary = "344"; decimalVal = ToDecimal(binary, 5); Console.WriteLine("Base5 value of decimal {0} is '{1}'", @decimal, binaryVal); Console.WriteLine("Decimal value of base5 '{0}' is {1}", binary, decimalVal); Console.WriteLine(); Console.WriteLine("And so forth.. excluding after base 10 (decimal) though :)"); Console.WriteLine(); @decimal = 16; binaryVal = ToBinary(@decimal, 11); binary = "b"; decimalVal = ToDecimal(binary, 11); Console.WriteLine("Hexidecimal value of decimal {0} is '{1}'", @decimal, binaryVal); Console.WriteLine("Decimal value of Hexidecimal '{0}' is {1}", binary, decimalVal); Console.WriteLine(); Console.WriteLine("Uh oh.. this aint right :( ... but let's cheat :P"); Console.WriteLine(); @decimal = 11; binaryVal = Convert.ToString(@decimal, 16); binary = "b"; decimalVal = Convert.ToInt32(binary, 16); Console.WriteLine("Hexidecimal value of decimal {0} is '{1}'", @decimal, binaryVal); Console.WriteLine("Decimal value of Hexidecimal '{0}' is {1}", binary, decimalVal); Console.ReadLine(); } static string ToBinary(decimal number, int @base) { var round = 0; var reverseBinary = string.Empty; while (number > 0) { var remainder = number % @base; reverseBinary += remainder; round = (int)(number / @base); number = round; } var binaryArray = reverseBinary.ToCharArray(); Array.Reverse(binaryArray); var binary = new string(binaryArray); return binary; } static double ToDecimal(string binary, int @base) { var val = 0d; if (!binary.All(char.IsNumber)) return 0d; for (int i = 0; i < binary.Length; i++) { var @char = Convert.ToDouble(binary[i].ToString()); var pow = (binary.Length - 1) - i; val += Math.Pow(@base, pow) * @char; } return val; } } 

学习来源:

所有你需要知道的有关二进制https://www.mathsisfun.com/binary-number-system.html

一切你需要知道和更多 – 包括algorithm将十进制转换为二进制http://www.electronics-tutorials.ws/binary/bin_2.html

  static void Main(string[] args) { Console.WriteLine("Enter number for converting to binary numerical system!"); int num = Convert.ToInt32(Console.ReadLine()); int[] arr = new int[16]; //for positive integers if (num > 0) { for (int i = 0; i < 16; i++) { if (num > 0) { if ((num % 2) == 0) { num = num / 2; arr[16 - (i + 1)] = 0; } else if ((num % 2) != 0) { num = num / 2; arr[16 - (i + 1)] = 1; } } } for (int y = 0; y < 16; y++) { Console.Write(arr[y]); } Console.ReadLine(); } //for negative integers else if (num < 0) { num = (num + 1) * -1; for (int i = 0; i < 16; i++) { if (num > 0) { if ((num % 2) == 0) { num = num / 2; arr[16 - (i + 1)] = 0; } else if ((num % 2) != 0) { num = num / 2; arr[16 - (i + 1)] = 1; } } } for (int y = 0; y < 16; y++) { if (arr[y] != 0) { arr[y] = 0; } else { arr[y] = 1; } Console.Write(arr[y]); } Console.ReadLine(); } }