benadaephondelat
benadaephondelat

Reputation: 23

how to convert a number in binary to its decimal value using c#

!!!!ANSWERED!!!!

I need little help finishing my simple bit manipulation program.

  1. Asks the user for a number
  2. Print the number's binary representation
  3. Ask the user for bit position and bit value
  4. Apply the changes
  5. Displays the changes made by the user in binary
  6. ?????? Convert that changes into a number again. ??????????

I just need a little help with step 5, as you will see from my code I'm a complete noob, so please do not laugh :) Any help will be deeply appreciated.

Console.WriteLine("Enter integer number");
int number = Convert.ToInt32(Console.ReadLine());
string binaryString = Convert.ToString(number, 2);
Console.WriteLine("The binary representation of {0} is", number);
Console.WriteLine(binaryString.PadLeft(16, '0'));


BitArray b = new BitArray(new int[] { number }); 


Console.WriteLine("Enter bit's position (0 to 15)");
int position = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("Enter bit's value (true or false)");
bool value = Convert.ToBoolean(Console.ReadLine());

b.Set(position, value); //set value to given position based on input from the user

Console.WriteLine("Your changes transformed \n{0} \nto ",binaryString.PadLeft(16,'0'));

for (int i = 15; i >= 0; i--) 
{
    if (b[i] == true)
    {
        Console.Write(1);
    }
    else
    {
        Console.Write(0);
    }
}
Console.WriteLine();

Upvotes: 2

Views: 1720

Answers (3)

Steve
Steve

Reputation: 216243

Convert.ToInt32 has an overload that takes the base used for the string representation of your integer

 Console.WriteLine(Convert.ToInt32(binaryString, 2))

However, after you have changed the bits inside the BitArray, to use the Convert.ToInt32 you need something to reconvert your bit array to a string

This code is adapted from another answer here on SO

.....
binaryString = ToBitString(b);
Console.WriteLine(Convert.ToInt32(binaryString, 2))
.....


public string ToBitString(BitArray bits)
{
    var sb = new StringBuilder();
    for (int i = bits.Count - 1; i>= 0; i--)
    {
        char c = bits[i] ? '1' : '0';
        sb.Append(c);
    }
    return sb.ToString();
}

Upvotes: 2

Taher  Rahgooy
Taher Rahgooy

Reputation: 6696

Change the final loop to the following:

int v = 0;
for (int i = 15; i >= 0; i--)
{
   if (b[i] == true)
   {
       Console.Write(1);
       v += (int)Math.Pow(2, i);
   }
   else
   {
       Console.Write(0);
   }
}

Upvotes: 0

Selman Genç
Selman Genç

Reputation: 101680

Store the binary representation into a variable:

string binary = binaryString.PadLeft(16, '0');
Console.WriteLine(binary);

Then get the position and value:

 Console.WriteLine("Enter bit's position (0 to 15)");
 int position = Convert.ToInt32(Console.ReadLine());

 Console.WriteLine("Enter bit's value (true or false)");
 bool value = Convert.ToBoolean(Console.ReadLine());

Make the changes:

var chars = binary.ToCharArray();
chars[position] = value ? '1' : '0';
binary = new string(chars);

Display the new binary representation and it's decimal equivelant:

Console.WriteLine("New binary value: {0}",binary);
Console.WriteLine("New decimal value: {0}", Convert.ToInt32(binary, 2));

Upvotes: 0

Related Questions