how to convert a number in binary to it's decimal value using c# -
!!!!answered!!!!
i need little finishing simple bit manipulation program.
- asks user number
- print number's binary representation
- ask user bit position , bit value
- apply changes
- displays changes made user in binary
- ?????? 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
Post a Comment