55

I would like to convert a binary number writen in a String into its integer value.

For example:

string input = "0101";
int output = convert(input);

output should be equal to 5

4
  • 4
    You should try googling "C# convert X to Y" before asking. Commented Feb 5, 2012 at 13:49
  • possible duplicate of Binary String to Integer Commented Feb 5, 2012 at 13:54
  • @TimLloyd the other question is newer? If anything that question is a dupe of this :) Commented Mar 31, 2017 at 4:35
  • @mhvelplund The question I linked to was asked on "Aug 13 '09". Commented Apr 3, 2017 at 11:50

1 Answer 1

127

Convert.ToInt32(String, Int32) lets you specify the base:

int output = Convert.ToInt32(input, 2);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.