Convert string representation of binary number to int in C#

前端 未结 2 782
我寻月下人不归
我寻月下人不归 2020-12-21 00:12

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 l

2条回答
  •  温柔的废话
    2020-12-21 00:31

    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)
    

提交回复
热议问题