C# Programming Question
Please provide the C# codes to convert a binary number to a decimal number
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Sanjeeb LenkaPosted Sep 26, 2013, 6:19 AM
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();
}
}
}
Sanjeeb LenkaPosted Sep 26, 2013, 6:01 AM
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();
}
}
}
Prasenjit DeyPosted Sep 26, 2013, 5:54 AM
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);