0

I have two string namely string1 and string2. they contain binary numbers and I want to manipulate them with different kinds of operations.

for example:

      string string1 = "1000";
      string string2 = "1011";

how can I Add/Subtract string1 and string2 so that:

      AnswerToAddtion = 10011               //1000 + 1011
      AnswerToSubtratction = 1101           //1000-1011
5
  • Try converting them to bytes Commented Dec 4, 2012 at 14:46
  • Same solution as: convert a binary string-representation to a byte array Commented Dec 4, 2012 at 14:47
  • 1
    Do you want the result of addition/subtraction in a string? Commented Dec 4, 2012 at 14:47
  • 2
    1000 - 1011 does not equal 1101. Commented Dec 4, 2012 at 14:57
  • This seems to be close to what you're looking for stackoverflow.com/a/2252904/930878 Commented Dec 4, 2012 at 15:22

1 Answer 1

5

You can use Convert.ToInt32("11011",2); to parse your string to an integer value.

To convert an integer to a binary string, you can use Convert.ToString(myInt, 2);

Sign up to request clarification or add additional context in comments.

4 Comments

@ChristopherRayl do you want the result of addition/subtraction in a string? The OP states they want to manipulate the values with different operations, and the problem is the data type is string. I think the solution is to use a more suiting data type.
I think he just wants to convert a binary string into a binary number so that he can perform binary arithmetic on it.
@ChristopherRayl what difference should it make to do binary operations on an int32? What data type is a binary type? Int32 should be sufficient. If a binary string result is desired, generating the string is simple enough.
I think (s)he's looking for something like: stackoverflow.com/a/2252904/930878

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.