how to convert a number in binary to it's decimal value using c# -


!!!!answered!!!!

i need little finishing simple bit manipulation program.

  1. asks user number
  2. print number's binary representation
  3. ask user bit position , bit value
  4. apply changes
  5. displays changes made user in binary
  6. ?????? convert changes number again. ??????????

i need little step 5 , see code i'm complete noob , please not laugh :) 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 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 given position based on input user          console.writeline("your changes transformed \n{0} \nto ",binarystring.padleft(16,'0'));          (int = 15; >= 0; i--)          {             if (b[i] == true)             {                 console.write(1);             }             else             {                 console.write(0);             }          }         console.writeline(); 

convert.toint32 has overload takes base used string representation of integer

 console.writeline(convert.toint32(binarystring, 2)) 

however, after have changed bits inside bitarray, use convert.toint32 need reconvert bit array string

this code adapted another answer here on so

..... binarystring = tobitstring(b); console.writeline(convert.toint32(binarystring, 2)) .....   public string tobitstring(bitarray bits) {     var sb = new stringbuilder();     (int = bits.count - 1; i>= 0; i--)     {         char c = bits[i] ? '1' : '0';         sb.append(c);     }     return sb.tostring(); } 

Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -