Computing.Net > Forums > Programming > dec to binary conversion help

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

dec to binary conversion help

Reply to Message Icon

Name: donever
Date: August 9, 2009 at 19:51:54 Pacific
OS: windows pro x64
CPU/Ram: athlon 4g
Product: Custom / CUSTOM BUILD
Subcategory: Theory
Comment:

///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



Sponsored Link
Ads by Google

Response Number 1
Name: donever
Date: August 9, 2009 at 22:00:52 Pacific
Reply:

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();
            } 
         
    }   


0
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: dec to binary conversion help

decimal to binary conversion help!! www.computing.net/answers/programming/decimal-to-binary-conversion-help/4822.html

DEC to HEX conversion? www.computing.net/answers/programming/dec-to-hex-conversion/3613.html

array to binary in c++? www.computing.net/answers/programming/array-to-binary-in-c/2981.html