0

I know how to convert an integer from decimal to fixed length binary string:

int number = 3;
int toBase = 2;
int length = 8;
Convert.ToString(number, toBase).PadLeft(length, '0')

Output:

00000011

How to assign the individual elements of that binary string to either int (or bool) array, such that after the conversion the array will look like1:

int[] binary = {0, 0, 0, 0, 0, 0, 1, 1}

or

bool[] binary = {false, false, false, false, false, false, true, true};

1. Using the facilities and not trivial for loop with char to int (or bool) type conversions.

Ziezi
  • 6,375
  • 3
  • 39
  • 49

5 Answers5

1

If you store the string you have created, for example

string theBinaryString = Convert.ToString(number, toBase).PadLeft(length, '0');
int[] binary = new int[8];
for(int i = 0; i < 8; i++)
{
    binary[i] = int.parse(theBinaryString[i].ToString());
}

Once the loop has finished you will have the array you are looking for, the ToString() is required as selecting from a string as if it was an array returns a char, which cannot be parsed into an int.

Alfie Goodacre
  • 2,753
  • 1
  • 13
  • 26
1

You can add some Linq to represent the string as an array:

string source = Convert.ToString(number, toBase).PadLeft(length, '0');

...

int[] binary = source.Select(c => c - '0').ToArray();

...

bool[] binary = source.Select(c => c == '1').ToArray();

Or you can compute arrays directly:

int[] binary = Enumerable
  .Range(1, length)
  .Select(i => number / (1 << (length - i)) % 2)
  .ToArray();

bool[] binary = Enumerable
  .Range(1, length)
  .Select(i => number / (1 << (length - i)) % 2 == 1)
  .ToArray(); 
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
1

maybe like this;

"1000110101110".ToCharArray().Select(c => c - '0').ToArray()

will give you:

int[13] { 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0 }
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
user734028
  • 1,021
  • 1
  • 9
  • 21
  • `48` - magic number which is a *bad parctice*; put `'0'` instead – Dmitry Bychenko Oct 14 '16 at 09:52
  • when reading your code, it is quite *unclear* what `48` stands for. Put `'0'`, i.e. `c => c - '0'` to make it evident: current character minus `'0'` – Dmitry Bychenko Oct 14 '16 at 10:01
  • while i kind of get what you are saying I was of the thinking since we are doing programming and the chararray function is being used it should be pretty evident that 48 is an integer being subtracted from c, a character. You can make a edit if you like. – user734028 Oct 14 '16 at 10:12
1

You can do it without converting the number to binary string:

var num = 123;
var bits = Enumerable.Range(0, 8).Select(i => (num >> (7-i)) & 1).ToArray();

The idea is to shift the number right sequentially by a decreasing number of positions, and mask off all bits except least significant one.

Demo.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Thank you for the answer! That is for `int` array right? – Ziezi Oct 14 '16 at 10:04
  • 1
    @Ziezi Yes, this produces an array of `int`s. If you want an array of `char`s, add a conditional to `Select` expression, or add result to `'0'` to convert an `int` to `char` digit. – Sergey Kalinichenko Oct 14 '16 at 10:09
0
int[] intArray = stringNumber.ToCharArray().Select(n => Convert.ToInt32(n)).ToArray();
mybirthname
  • 17,949
  • 3
  • 31
  • 55