I have the following two-dimensional int
array:
int[][] array = new int[128][128];
This two-dimensional array contains only zeros and ones. I want to read each line and make a byte[]
(byte array) out of its contents. For instance, let's say that the first line is: 0101111000111...101
, consisting of 128 numbers. I want this line to be a byte[]
of 128 bits (which means 16 bytes).
What's the most efficient way to transform each line into a byte array? Bear in mind that maintaining the size is important. Since each lines consists of 128 "bits" the byte array should have a size of 16 bytes (128/8).
One way I've thought on how to do this is to make each line into a BigInteger and then convert it into byte array but unfortunately I can't produce the proper results. I've also tried some of the other options available to StackOverflow to no avail. For instance this solution produces an output of 512 and I don't understand why.
For the above reason I don't consider this post a duplicate since the various asks and answers don't take in mind the size of the byte array and its correlation to the int array.