dec to binary conversion help

Custom / CUSTOM BUILD
August 9, 2009 at 19:51:54
Specs: windows pro x64, athlon 4g
///Simple console application to convert decimal value to binary value
///When I try to debug, it doesn't run; can anybody help out. I'm using
///C# 2008 express edition. the problem is line 14 and 99
using System;

class Program
{
static void Main(string[] args)
{
int valueOne;
Console.WriteLine("please enter number to convert to binary");
valueOne = Convert.ToInt32(Console.ReadLine());

CalculateBinary( valueOne ); //line 14

private void CalculateBinary(int value)
{
int BinaryValue;

if (128 > value)
{
BinaryValue &= 0;
}
else if (128 <= value)
{
BinaryValue &= 1;
value -= 128;
}
if (64 > value)
{
BinaryValue &= 0;
}
else if (64 <= value)
{
BinaryValue &= 1;
value -= 64;
}
if (32 > value)
{
BinaryValue &= 0;
}
else if (32 <= value)
{
BinaryValue &= 1;
value -= 32;
}
if (16 > value)
{
BinaryValue &= 0;
}
else if (16 <= value)
{
BinaryValue &= 1;
value -= 16;
}
if (8 > value)
{
BinaryValue &= 0;
}
else if (8 <= value)
{
BinaryValue &= 1;
value -= 8;
}
if (4 > value)
{
BinaryValue &= 0;
}
else if (4 <= value)
{
BinaryValue &= 1;
value -= 4;
}
if (2 > value)
{
BinaryValue &= 0;
}
else if (2 <= value)
{
BinaryValue &= 1;
value -= 2;
}
if (1 > value)
{
BinaryValue &= 0;
}
else if (1 <= value)
{
BinaryValue &= 1;
value -= 1;
}

Console.WriteLine("Decimal\t\tBinary");
Console.WriteLine("{0}{1}", valueOne, BinaryValue);
Console.ReadLine();
}

}
} //line 99


See More: dec to binary conversion help

Report •


#1
August 9, 2009 at 22:00:52
I figure it out. the problem is that I place the function in the wrong place. I modified the code but another problem that I have is this BinaryValue &= 1;

base on the above command. let say that BinaryValue =00 and then it encounter the above command; is the BinaryValue now should be 001 instead.

using System;

    class Program
    {
        static void Main(string[] args)
        {
            int valueOne;
            Console.WriteLine("please enter number to convert to binary");
            valueOne = Convert.ToInt32(Console.ReadLine());
            Calculate(valueOne);
         
        }
         public static void Calculate(int value)
            {
                int BinaryValue = 0;

                if (128 > value)
                {
                    BinaryValue &= 0;
                }
                else if (128 <= value)
                {
                    BinaryValue &= 1;
                    value-= 128;
                }
                if (64 > value)
                {
                    BinaryValue &= 0;
                }
                else if (64 <= value)
                {
                    BinaryValue &= 1;
                    value -= 64;
                }
                if (32 > value)
                {
                    BinaryValue &= 0;
                }
                else if (32 <= value)
                {
                    BinaryValue &= 1;
                    value -= 32;
                }
                if (16 > value)
                {
                    BinaryValue &= 0;
                }
                else if (16 <= value)
                {
                    BinaryValue &= 1;
                    value -= 16;
                }
                if (8 > value)
                {
                    BinaryValue &= 0;
                }
                else if (8 <= value)
                {
                    BinaryValue &= 1;
                    value -= 8;
                }
                if (4 > value)
                {
                    BinaryValue &= 0;
                }
                else if (4 <= value)
                {
                    BinaryValue &= 1;
                    value -= 4;
                }
                if (2 > value)
                {
                    BinaryValue &= 0;
                }
                else if (2 <= value)
                {
                    BinaryValue &= 1;
                    value -= 2;
                }
                if (1 > value)
                {
                    BinaryValue &= 0;
                }
                else if (1 <= value)
                {
                    BinaryValue &= 1;
                    value -= 1;
                }
                Console.WriteLine("Binary");
                Console.WriteLine("{0}",  BinaryValue);
                Console.ReadLine();
            } 
         
    }   


Report •
Related Solutions


Ask Question