How can I convert a binary string, such as 1001101
to Decimal? (77)
I wanted a solution that always gave 32 bits no matter how big or small the number. So this is what I created.
public static string ConvertUintToBitString(uint Number)
{
string _BitString = string.Empty;
if (Number >= 2147483648)
{
_BitString += '1';
Number = Number - 2147483648;
}
else
{
_BitString += '0';
}
if (Number >= 1073741824)
{
_BitString += '1';
Number = Number - 1073741824;
}
else
{
_BitString += '0';
}
if (Number >= 536870912)
{
_BitString += '1';
Number = Number - 536870912;
}
else
{
_BitString += '0';
}
if (Number >= 268435456)
{
_BitString += '1';
Number = Number - 268435456;
}
else
{
_BitString += '0';
}
if (Number >= 134217728)
{
_BitString += '1';
Number = Number - 134217728;
}
else
{
_BitString += '0';
}
if (Number >= 67108864)
{
_BitString += '1';
Number = Number - 67108864;
}
else
{
_BitString += '0';
}
if (Number >= 33554432)
{
_BitString += '1';
Number = Number - 33554432;
}
else
{
_BitString += '0';
}
if (Number >= 16777216)
{
_BitString += '1';
Number = Number - 16777216;
}
else
{
_BitString += '0';
}
if (Number >= 8388608)
{
_BitString += '1';
Number = Number - 8388608;
}
else
{
_BitString += '0';
}
if (Number >= 4194304)
{
_BitString += '1';
Number = Number - 4194304;
}
else
{
_BitString += '0';
}
if (Number >= 2097152)
{
_BitString += '1';
Number = Number - 2097152;
}
else
{
_BitString += '0';
}
if (Number >= 1048576)
{
_BitString += '1';
Number = Number - 1048576;
}
else
{
_BitString += '0';
}
if (Number >= 524288)
{
_BitString += '1';
Number = Number - 524288;
}
else
{
_BitString += '0';
}
if (Number >= 262144)
{
_BitString += '1';
Number = Number - 262144;
}
else
{
_BitString += '0';
}
if (Number >= 131072)
{
_BitString += '1';
Number = Number - 131072;
}
else
{
_BitString += '0';
}
if (Number >= 65536)
{
_BitString += '1';
Number = Number - 65536;
}
else
{
_BitString += '0';
}
if (Number >= 32768)
{
_BitString += '1';
Number = Number - 32768;
}
else
{
_BitString += '0';
}
if (Number >= 16384)
{
_BitString += '1';
Number = Number - 16384;
}
else
{
_BitString += '0';
}
if (Number >= 8192)
{
_BitString += '1';
Number = Number - 8192;
}
else
{
_BitString += '0';
}
if (Number >= 4096)
{
_BitString += '1';
Number = Number - 4096;
}
else
{
_BitString += '0';
}
if (Number >= 2048)
{
_BitString += '1';
Number = Number - 2048;
}
else
{
_BitString += '0';
}
if (Number >= 1024)
{
_BitString += '1';
Number = Number - 1024;
}
else
{
_BitString += '0';
}
if (Number >= 512)
{
_BitString += '1';
Number = Number - 512;
}
else
{
_BitString += '0';
}
if (Number >= 256)
{
_BitString += '1';
Number = Number - 256;
}
else
{
_BitString += '0';
}
if (Number >= 128)
{
_BitString += '1';
Number = Number - 128;
}
else
{
_BitString += '0';
}
if (Number >= 64)
{
_BitString += '1';
Number = Number - 64;
}
else
{
_BitString += '0';
}
if (Number >= 32)
{
_BitString += '1';
Number = Number - 32;
}
else
{
_BitString += '0';
}
if (Number >= 16)
{
_BitString += '1';
Number = Number - 16;
}
else
{
_BitString += '0';
}
if (Number >= 8)
{
_BitString += '1';
Number = Number - 8;
}
else
{
_BitString += '0';
}
if (Number >= 4)
{
_BitString += '1';
Number = Number - 4;
}
else
{
_BitString += '0';
}
if (Number >= 2)
{
_BitString += '1';
Number = Number - 2;
}
else
{
_BitString += '0';
}
if (Number == 1)
{
_BitString += '1';
}
else
{
_BitString += '0';
}
return _BitString;
}
Take a look at this questions which is very similar but dealing with hex How to convert numbers between hexadecimal and decimal in C#?
Convert.ToInt64(value, 2)
string s=Console.ReadLine();
int b=Convert.ToInt32(s,2);
Console.WriteLine("Input value in base 10 = "+b);
convert any binary to decimal. :)
If you're after a manual way instead of using built in C# libraries, this would work:
static int BinaryToDec(string input)
{
char[] array = input.ToCharArray();
// Reverse since 16-8-4-2-1 not 1-2-4-8-16.
Array.Reverse(array);
/*
* [0] = 1
* [1] = 2
* [2] = 4
* etc
*/
int sum = 0;
for(int i = 0; i < array.Length; i++)
{
if (array[i] == '1')
{
// Method uses raising 2 to the power of the index.
if (i == 0)
{
sum += 1;
}
else
{
sum += (int)Math.Pow(2, i);
}
}
}
return sum;
}
The Convert.ToInt32
method has an overload that accepts a base parameter.
Convert.ToInt32("1001101", 2).ToString();
I have tried this after reading your problem. It is a bit longer but it provides a solution. I saved binary elements in an array to get a solution. Like I said It is a bit longer, much shorter ways can be found.
// Binary ------> Decimal
int len;
double deci = 0;
Console.Write("Length of binary number: ");
len = Convert.ToInt32(Console.ReadLine());
int[] bnry = new int[len];
for (int i = 0; i < len; i++)
{
Console.Write("{0} index of binary number: ", i);
bnry[i] = Convert.ToInt32(Console.ReadLine());
}
Console.Write("Your binary number: ");
for (int i = len - 1; 0 <= i; i--)
{
Console.Write(bnry[i]);
}
Console.Write("\nDecimal number: ");
for (int i = 0; i < len; i++)
{
deci += (bnry[i] * Math.Pow(2, i));
}
Console.Write(deci);