5

I have a string of eight 1s and 0s with spaces in between, something like "1 0 0 1 1 0 1 0", that I want converted in to an int. Is there a simple way to do this? I feel like some kind of linq parsing would do it, but I don't even know what to do with the characters once I find them.

1
  • Just to clarify, the string 1 0 0 1 1 0 1 0 should be parsed as two, four bit values: 1001 and 1010. Then converted to hex. Correct? Commented Jan 10, 2013 at 17:57

2 Answers 2

14

You don't need any LINQ.
Convert.ToInt*() takes an optional fromBase parameter, which must be 2, 8, 10, or 16.

Convert.ToInt32("1 0 0 1 1 0 1 0".Replace(" ", ""), 2)
Sign up to request clarification or add additional context in comments.

1 Comment

Well that makes this a very simple problem then hah. Thanks so much!
1

An alternative to @SLaks's answer (but only for parsing Hex) is

Int32.Parse(hexString, System.Globalization.NumberStyles.HexNumber);

There's no equivalent for binary, though, so his is a better general-purpose answer.

1 Comment

I was mistaken. Removed earlier comment.

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.