Java Boolean Convert to booleanArrayToBytes(boolean[] input)

Here you can find the source of booleanArrayToBytes(boolean[] input)

Description

A method that performs the same function as the booleanArrayToByte() method but is not limited by input and output size.

License

Open Source License

Declaration

public static byte[] booleanArrayToBytes(boolean[] input) 

Method Source Code

//package com.java2s;
/**/*from w  w  w. j  a  v a  2s.c  o m*/
 * <PRE>
 * Name   : com.solidmatrix.regxmaker.util.shared.ArrayUtils
 * Project: RegXmaker Library
 * Version: 1.1
 * Tier   : N/A (Function Class)
 * Author : Gennadiy Shafranovich
 * Purpose: General utilities for array searching and matching
 *
 * Copyright (C) 2001, 2004 SolidMatrix Technologies, Inc.
 * This file is part of RegXmaker Library.
 *
 * RegXmaker Library is is free software; you can redistribute it and/or modify
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * RegXmaker library 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
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Comments: Full, with javadoc.
 *
 * Modification History
 *
 * 02-19-2001 GS Created
 *
 * 02-19-2001 GS Ready for testing
 *
 * 02-23-2001 GS added byte to boolean convertion methods for patch library
 *
 * 06-18-2001 GS Fixed possible error in indexOf() methods.
 *
 * 06-21-2001 GS Fixed bug in indexOf() method. Values seemed to be
 *               offset by one so -1 was inserted in many conditional
 *               statements.
 *
 * 07-01-2001 GS Fixed bug in indexOf() method that cause an infinite
 *               loop while searching.
 *
 * 07-05-2004 YS Added licensing information
 * </PRE>
 */

public class Main {
    /**********************************************************
     *                  Boolean-Byte section                  *
     *                                                        *
     * This section contains methods responsible for handling *
     * the convertion of flag arrays (boolean) into bytes     *
     * that may be writen to outside files or other streams   *
     *                                                        *
     **********************************************************/

    public static final byte[] BIT_FLAGS = { (byte) 1, (byte) 2, (byte) 4, (byte) 8, (byte) 16, (byte) 32,
            (byte) 64, (byte) 128 };

    /**
     * A method that performs the same function as the booleanArrayToByte()
      * method but is not limited by input and output size. This method
      * can take an ultimited length boolean array and will convert it to 
      * an array of bytes equal in length to the function 
      * ceiling(size of input array / 8.0)
     *
     */
    public static byte[] booleanArrayToBytes(boolean[] input) {
        int count = input.length / 8;
        if (input.length % 8 != 0)
            count++;

        byte[] out = new byte[count];

        int pos = 0;
        int bpos = 0;
        while (bpos < count) {
            out[bpos] = 0;

            for (int temp = 0; temp < 8 && pos < input.length; temp++, pos++) {
                if (input[pos])
                    out[bpos] = (byte) (out[bpos] | BIT_FLAGS[temp]);
            } //for

            bpos++;
        } //while

        return out;
    }
}

Related

  1. boolean2bin(boolean b)
  2. boolean2byte(boolean b)
  3. boolean2double(boolean b)
  4. boolean2long(boolean b)
  5. booleanArrayToBitString(final boolean[] booleanArray)
  6. booleanArrayToInt(boolean[] in)
  7. booleanArrayToIntIndexes(boolean[] arrb)
  8. booleanArrayToString(boolean[] array)
  9. booleanArrayToString(boolean[] b, String delim)