Java Bits to Int bitsToIntArray(String bits, int min, int max, int numBits, int size)

Here you can find the source of bitsToIntArray(String bits, int min, int max, int numBits, int size)

Description

Turns a bit string (0s and 1s) into an integer array.

License

Open Source License

Parameter

Parameter Description
bits the string
min the minimum
max the maximum
numBits the number of bits
size the size of the array

Return

the reconstructed integer array

Declaration

public static int[] bitsToIntArray(String bits, int min, int max, int numBits, int size) 

Method Source Code

//package com.java2s;
/*// w  w w.  j  a v a 2  s . co m
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Turns a bit string (0s and 1s) into an integer array.
     *
     * @param bits   the string
     * @param min      the minimum
     * @param max      the maximum
     * @param numBits   the number of bits
     * @param size   the size of the array
     * @return      the reconstructed integer array
     */
    public static int[] bitsToIntArray(String bits, int min, int max, int numBits, int size) {
        int ret[] = new int[size];
        for (int k = 0; k < size; k++) {
            int start = numBits * k;
            double j = 0;
            for (int i = start; i < start + numBits; i++) {
                if (bits.charAt(i) == '1') {
                    j = j + Math.pow(2, start + numBits - i - 1);
                }
            }
            j += min;
            ret[k] = (Math.min((int) j, max));
        }
        return ret;
    }
}

Related

  1. bitsToInt(boolean[] pBits)
  2. bitsToInt(String bits, int min, int max)