C
C#•2y ago
GIGA BRAIN

Binary To Decimal Program Help

Hi I'm having trouble trying to determine whether or not the current index in my inputted binary string is a 0 or 1. I've been trying to look for different ways to figure out how to do this, but I get stumped every time. Can someone push me in the right direction? Thank you.
// Arrays may not be used to contain the binary digits entered by the user

// Determining the decimal equivalent of a particular binary digit in the sequence must be calculated using a single mathematical function,
// for example the natural logarithm. It's up to you to figure out which function to use.

// User can enter up to 8 binary digits in one input field
// User must be notified if anything other than a 0 or 1 was entered
// User views the results in a single output field containing the decimal (base 10) equivalent of the binary number that was entered

// int numberInput = 0;
Console.WriteLine("Input up to 8 binary digits: ");
String userInput = Console.ReadLine();
// bool isParseable = Int32.TryParse(userInput, out numberInput);
int index = 0;
int finalResult = 0;

for (int i=0; i < userInput.Length; i++)
{
if (userInput[i] = "0")
{
GetPow(2, index);
finalResult += index;
System.Console.WriteLine(finalResult);
index++;
}
}

static int GetPow(int baseNum, int powNum)
{
int result = 1;

for (int i = 0; i < powNum; i++)
{
result = result * baseNum;
}

return result;
}
// Arrays may not be used to contain the binary digits entered by the user

// Determining the decimal equivalent of a particular binary digit in the sequence must be calculated using a single mathematical function,
// for example the natural logarithm. It's up to you to figure out which function to use.

// User can enter up to 8 binary digits in one input field
// User must be notified if anything other than a 0 or 1 was entered
// User views the results in a single output field containing the decimal (base 10) equivalent of the binary number that was entered

// int numberInput = 0;
Console.WriteLine("Input up to 8 binary digits: ");
String userInput = Console.ReadLine();
// bool isParseable = Int32.TryParse(userInput, out numberInput);
int index = 0;
int finalResult = 0;

for (int i=0; i < userInput.Length; i++)
{
if (userInput[i] = "0")
{
GetPow(2, index);
finalResult += index;
System.Console.WriteLine(finalResult);
index++;
}
}

static int GetPow(int baseNum, int powNum)
{
int result = 1;

for (int i = 0; i < powNum; i++)
{
result = result * baseNum;
}

return result;
}
8 Replies
mtreit
mtreit•2y ago
if (userInput[i] = "0")
if (userInput[i] = "0")
A single equal sign is assignment in C#. Use two equals signs for equality.
GIGA BRAIN
GIGA BRAINOP•2y ago
Yeah I've tried both, but it says I can't use "==" for char and string types Should I be converting something to the char/string type so that it can work?
mtreit
mtreit•2y ago
Use single quotes for a char literal, not double quotes
GIGA BRAIN
GIGA BRAINOP•2y ago
oooooooh yeah that fixed it, thanks! would you say that i'm heading in the right direction for this program?
mtreit
mtreit•2y ago
Unclear 🙂
GIGA BRAIN
GIGA BRAINOP•2y ago
Got it, will work on this more. Thanks for the help 🙂
mtreit
mtreit•2y ago
You can probably use the Math.Pow function instead of re-implementing that yourself, btw.
GIGA BRAIN
GIGA BRAINOP•2y ago
Ooh for sure i'll definitely look into that
Want results from more Discord servers?
Add your server