Sign Up
RelaxBuddy helps you connect and share with the people in your life.

decimal to binary conversion in c

How to transform a decimal no into binary in c #

Now I'm the usage of like:

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

But it is throwing a few Exceptions.

How to try this?

#c
Last update on March 21, 12:39 am by CRUZ1618.
malik2956
Are you trying to convert the string representation of a number, or an actual number? And are you trying to convert to decimal, or int? Your example doesn't really match your question.
  • November 11, 2016
  • ·
ALPHONSO5495
If you're looking to convert decimal to bytes, you can use this code: gist.github.com/eranbetzalel/�
  • November 11, 2016
  • ·

Your instance has an integer expressed as a string. Let's say your integer was once in reality an integer, and also you wish to need to take the integer and convert it to a binary string.

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

Which returns one thousand.

Last update on March 21, 12:39 am by jerry3970.
HERBERT9441
is there any similar method to convert binary to decimal?
  • November 11, 2016
  • ·
WHITNEY7792
@kashif int value = Convert.ToInt32("1101", 2) would give value the value 13.
  • November 11, 2016
  • ·

<sturdy>**Convert from any vintage base to any base in C#**

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

Supported bases are 2, eight, 10 and sixteen

Last update on March 21, 12:39 am by leroy.
hosea
This will not work. I simply tried string binary = Convert.ToString(533, 26); and got a ArgumentException: Invalid base
  • November 11, 2016
  • ·
grant
Yep, from MSDN: only classic bases are supported msdn.microsoft.com/en-us/library/8s62fh68(v=vs.110).aspx toBase Type: System.Int32 The base of the return value, which must be 2, 8, 10, or 16.
  • November 11, 2016
  • ·

Very Simple with out a different code, simply enter, conversion and output.

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); } }}
Last update on March 21, 12:39 am by harvey2205.
russell6227
+1 for the classic solution
  • November 11, 2016
  • ·
BOYCE3250
For a generic alphabet, this should be do{ [...] }while(decimalNumber > 0);
  • November 11, 2016
  • ·

http://zamirsblog.blogspot.com/2011/10/convert-dec...

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; }
Last update on March 21, 12:39 am by DARRYL5448.
rolf8248
Not sure how this differs from Xenon's answer.
  • November 11, 2016
  • ·
DREW7099
He answered this before Xenon
  • November 11, 2016
  • ·

Convert.ToInt32(string, base) does now not do base conversion into your base. It assumes that the string accommodates a legitimate quantity within the indicated base, and converts to base 10.

So you might be getting an mistakes as a result of "eight" isn't a legitimate digit in base 2.

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

Will display 15 (1111 base 2 = 15 base 10)

String str = "f000";String Ans = Convert.ToInt32(str, 16).ToString();

Will display 61440.

Last update on March 21, 12:39 am by maxwell2918.
  • January 1, 1970
  • ·
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 needInt64 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;}
Last update on March 21, 12:39 am by CORNELL3899.
JOSE8081
You're reinventing the wheel here. The BCL already contains methods to do this.
  • November 11, 2016
  • ·
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
Last update on March 21, 12:39 am by WELDON1091.
  • January 1, 1970
  • ·

I know this answer would look very similar to the various answers already here, but I noticed just about none of them uses a for-loop. This code works, and can also be thought to be simple, in the sense it is going to work without any special functions, like a ToString() with parameters, and is not too long as well. Maybe some prefer for-loops as an alternative of just even as-loop, this can be suitable for them.

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=""></=>
Last update on March 21, 12:39 am by lee.
  • January 1, 1970
  • ·
// 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; }
Last update on March 21, 12:39 am by JAROD7389.
  • January 1, 1970
  • ·

primitive manner:

public string ToBinary(int n){ if (n < 2)="" return="" n.tostring();="" var="" divisor="n" 2;="" var="" remainder="n" %="" 2;="" return="" tobinary(divisor)="" +="">
Last update on March 21, 12:39 am by XAVIER8445.
  • January 1, 1970
  • ·
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 Smile"); 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 Unhappy ... but let's cheat Tongue"); 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[].ToString());" var="" pow="(binary.Length" -="" 1)="" -="" i;="" val="" +="Math.Pow(@base," pow)="" *="" @char;="" }="" return="" val;="">

Learning assets:

Everything you must find out about binaryhttps://www.mathsisfun.com/binary-quantity-g...

Everything you wish to need to understand and extra - together with set of rules to transform decimal to binary http://www.electronics-tutorials.ws/binary/bin_2.h...

Last update on March 21, 12:40 am by HARRISON3612.
  • January 1, 1970
  • ·
Latest News
All times are GMT. The time now is 9:15 am.