2

I want to convert binary value into integer array in c#.

For example consider binary value : 111 , it's integer equivalent is 7 (right most digit equals to integer 1, middle binary digit is equivalent to integer 2 and left most digit is 4, so 1+2+4 = 7).

how do I get each integer digit (i.e. 1, 2 ,4) in form of an array (or list) using c# ?

Pinal Dave
  • 533
  • 4
  • 14
  • 27
  • Can you rephrase your question.. ? Do you simply want the binary places? Or do you want the value 7 from the binary representation .. ? Provide some pseudo-code or something so we can see what you're after. – Simon Whitehead Jan 04 '14 at 23:20
  • 1
    Why would you not just want the integer, and extract bits from that as required? – Jon Skeet Jan 04 '14 at 23:21
  • possible duplicate of [Convert binary string into integer](http://stackoverflow.com/questions/9149728/convert-binary-string-into-integer) – Robert Rouhani Jan 05 '14 at 08:10

3 Answers3

3

Like this:

string binaryString = "111";
var integerValue = Convert.ToInt64(binaryString,2);

integerValue will be 7 now.

Update, thanks to the comments:

If you want to store each value then you need to go through the string step by step in a for loop and bit-shift (<< operator) to get to your desired outcome.

[Test]
public void BinaryStringToValues()
{
    const string binaryString = "111";
    var values = new List<int>();
    for (var i = 0; i < binaryString.Length; i++)
    {
        if (binaryString[binaryString.Length - i - 1] == '0')
        {
            continue;
        }
        values.Add(1 << i);
    }

    Assert.AreEqual(1, values[0]);
    Assert.AreEqual(2, values[1]);
    Assert.AreEqual(4, values[2]);
}

The test will pass.

bas
  • 13,550
  • 20
  • 69
  • 146
  • 1
    While this answers converting from binary .. I don't know how it relates to "how do I get each integer digit (i.e. 1, 2 ,4)".. or what that even means. – Simon Whitehead Jan 04 '14 at 23:23
  • Why do I have the feeling I am doing somebodies homework? :) – bas Jan 04 '14 at 23:31
  • 1
    Probably because only a teacher would dream up an exercise as silly as this one... or you've simply been trolled. Either way, have a +1 – NotMe Jan 04 '14 at 23:37
  • If you like, you can use `1 << i` instead of `(int)Math.Pow(2, i)`. – Jeppe Stig Nielsen Jan 04 '14 at 23:53
  • @JeppeStigNielsen :)), now I have the feeling I am back in university! Thx, looks much cooler this way! :) long time I used bit shifting – bas Jan 05 '14 at 09:46
3
string bin = "1011";

var str = String.Join(",", bin.Reverse().Select((c, i) => (c - '0') * (1 << i)));

str will be 1,2,0,8. if you want the result as a list

var list = bin.Reverse().Select((c, i) => (c - '0') * (1 << i)).ToList();
EZI
  • 15,209
  • 2
  • 27
  • 33
2

It's not entirely clear what you want, but let's assume that from this string:

"1011011"

you want to get this array:

64, 16, 8, 2, 1

then you can use this code (LINQPad program):

void Main()
{
    string input = "1011011";

    int[] values = input
        .Select((value, idx) => value == '1'
                ? (1 << (input.Length - idx - 1))
                : 0)
        .Where(value => value > 0)
        .ToArray();

    values.Dump();
}

Output:

linqpad output

This will:

  • For each character that is '1'
  • Calculate the position, from the right, where the rightmost position is position 0
  • Then it will calculate 1 << x (x is that position), which will return 1 for the rightmost position, 2 for the one left of it, 4 for the one left of 2, 8 for the next, 16, 32, 64, and so on.
  • The array at this point would be [64, 0, 16, 8, 0, 2, 1] so we'll remove the zeroes
Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825