Here you can find the source of intArrayToBits(int[] ina, int min, int max, int numBits)
Parameter | Description |
---|---|
ina | the integer array |
min | the minimum |
max | the maximum |
numBits | the number of bits |
public static String intArrayToBits(int[] ina, int min, int max, int numBits)
//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(); } }