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.

2

5 Answers 5

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(); 
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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.

2 Comments

Thank you for the answer! That is for int array right?
@Ziezi Yes, this produces an array of ints. If you want an array of chars, add a conditional to Select expression, or add result to '0' to convert an int to char digit.
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 }

3 Comments

48 - magic number which is a bad parctice; put '0' instead
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'
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.
0
int[] intArray = stringNumber.ToCharArray().Select(n => Convert.ToInt32(n)).ToArray();

1 Comment

@viveknuna yep I fixed it. My bad

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.