Java Integer Array Convert To intArrayToBits(int[] ina, int min, int max, int numBits)

Here you can find the source of intArrayToBits(int[] ina, int min, int max, int numBits)

Description

Creates a bit string (0s and 1s) from an integer array.

License

Open Source License

Parameter

Parameter Description
ina the integer array
min the minimum
max the maximum
numBits the number of bits

Return

the bit string

Declaration

public static String intArrayToBits(int[] ina, int min, int max, int numBits) 

Method Source Code

//package com.java2s;
/*/*from ww w.  ja v  a 2  s  . com*/
 *   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 {
    /**
     * Creates a bit string (0s and 1s) from an integer array.
     *
     * @param ina      the integer array
     * @param min      the minimum
     * @param max      the maximum
     * @param numBits   the number of bits
     * @return      the bit string
     */
    public static String intArrayToBits(int[] ina, int min, int max, int numBits) {
        StringBuilder buff = new StringBuilder();
        for (int i = 0; i < ina.length; i++) {
            int in = ina[i];
            in = in - min;
            in = Math.min(in, max - min);
            String bits = Integer.toBinaryString(in);
            while (bits.length() < numBits)
                bits = "0" + bits;
            buff.append(bits);
        }
        return buff.toString();
    }
}

Related

  1. intArray2HashCode(int[] xs)
  2. intArray2HexString(int[] intArray)
  3. intArray2IntegerArray(int[] array, int newLength)
  4. intArray2Json(int[] array)
  5. intArrayToAddressString(int vals[])
  6. intArrayToCharArray(int[][] intArray)
  7. intArrayToFloatArray(int[] intArray)
  8. intArrayToIp(final int[] array, final int offset)
  9. intsToByteHighAndLow(int highValue, int lowValue)