When Converting decimal to binary or binary to decimal following function has been used.
binaryNum = Convert.ToString(decimalNum, 2);
i = Convert.ToInt32(binaryString, 2);
I wish to know what is 2 means. Problem is highlighted below.
using System;
namespace Converting_decimal_binary
{
class Program
{
static void Main(string[] args)
{
//Converting decimal to binary
int decimalNum = 20;
string binaryNum = Convert.ToString(decimalNum, 2);//10100
Console.WriteLine(binaryNum);
//Converting binary to decimal
//where binaryString is the binary number expressed as a string.
string binaryString = "10100";
int i = Convert.ToInt32(binaryString, 2);
Console.WriteLine(i);//20
Console.ReadKey();
}
}
}
Loading
VulpesPosted Oct 23, 2013, 5:29 AM
So 2 is the base of arithmetic to be used.
Other valid values are 8 (octal), 10 (decimal) and 16 (hexadecimal).
Posted Oct 23, 2013, 6:06 AM
VulpesPosted Oct 23, 2013, 5:54 AM
Similarly, Convert.ToInt32 needs to know what base is being used to represent the numeric string. A string of 10100 would clearly represent different integers in different bases.
Posted Oct 23, 2013, 5:47 AM