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
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; }
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();
}
}
Summary: An easier way to do conversions of decimal to binary is if you open up calculator in windows. Switch the mode to scientific you will see Four radial buttons right below the left side of the input/outp...
Summary: A few comments... You define your decimal input as a double...This program is not equipped to handle anything other than an integer decimal input. Besides, conversion of fractional numbers from Decim...
Summary: hi im using this function to read the current position of a mechanical arm.I was told this is the way to convert array elements to binary number.Is this procedure correct? it seems that when the movem...