C# Programming Question
Please provide the C# codes to convert a binary number to a decimal number
3 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Prasenjit DeyRank #6,279 · 271 reputationPosted Sep 26, 2013, 1:54 AM EDT
Decimal to Binary conversion:
int decimalNum = 42;
string binaryNum = Convert.ToString(decimalNum, 2);
Binary To Decimal conversion:
string binaryNum = "101";
int decimalNum = Convert.ToInt32(binaryNum , 2);
Sanjeeb LenkaRank #301 · 8.3K reputationPosted Sep 26, 2013, 2:01 AM EDT
For binary to decimal
try this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace IfExample
{
class binarytodecimal
{
static void Main()
{
Console.WriteLine("Enter a binary no to convert it to decimal");
string x = Console.ReadLine();
Console.WriteLine("decimal no={0}",Convert.ToInt32(x, 2).ToString());
Console.ReadLine();
}
}
}
Sanjeeb LenkaRank #301 · 8.3K reputationPosted Sep 26, 2013, 2:19 AM EDT
using System;
namespace IfExample
{
class binarytodecimal
{
static void Main()
{
Console.WriteLine("Enter a valid octal no to convert it to binary");
string x = Console.ReadLine();
int decValue = Convert.ToInt32(x, 8);//converting octal to decimal
string retVal = Convert.ToString(decValue, 2);//converting decimal to binary
Console.WriteLine("binary no={0}", retVal);
Console.ReadLine();
}
}
}