Java Byte Array Create toByteArray(boolean[] array)

Here you can find the source of toByteArray(boolean[] array)

Description

Coverts given booleans array to array of bytes.

License

Open Source License

Parameter

Parameter Description
array booleans array to convert.

Return

converted array of bytes.

Declaration

public static byte[] toByteArray(boolean[] array) 

Method Source Code

//package com.java2s;
/*//from   w  w w  .  j  a va2s  .c  o m
 * The MIT License (MIT)
 *
 * Copyright (c) 2016. Diorite (by Bart?omiej Mazur (aka GotoFinal))
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

public class Main {
    /**
     * Coverts given booleans array to array of bytes.
     *
     * @param array
     *         booleans array to convert.
     *
     * @return converted array of bytes.
     */
    public static byte[] toByteArray(boolean[] array) {
        byte[] result = new byte[array.length];
        for (int i = 0; i < array.length; i++) {
            result[i] = array[i] ? (byte) 1 : (byte) 0;
        }
        return result;
    }

    /**
     * Coverts given shorts array to array of bytes.
     *
     * @param array
     *         shorts array to convert.
     *
     * @return converted array of bytes.
     */
    public static byte[] toByteArray(short[] array) {
        byte[] result = new byte[array.length];
        for (int i = 0; i < array.length; i++) {
            result[i] = (byte) array[i];
        }
        return result;
    }

    /**
     * Coverts given chars array to array of bytes.
     *
     * @param array
     *         chars array to convert.
     *
     * @return converted array of bytes.
     */
    public static byte[] toByteArray(char[] array) {
        byte[] result = new byte[array.length];
        for (int i = 0; i < array.length; i++) {
            result[i] = (byte) array[i];
        }
        return result;
    }

    /**
     * Coverts given ints array to array of bytes.
     *
     * @param array
     *         ints array to convert.
     *
     * @return converted array of bytes.
     */
    public static byte[] toByteArray(int[] array) {
        byte[] result = new byte[array.length];
        for (int i = 0; i < array.length; i++) {
            result[i] = (byte) array[i];
        }
        return result;
    }

    /**
     * Coverts given longs array to array of bytes.
     *
     * @param array
     *         longs array to convert.
     *
     * @return converted array of bytes.
     */
    public static byte[] toByteArray(long[] array) {
        byte[] result = new byte[array.length];
        for (int i = 0; i < array.length; i++) {
            result[i] = (byte) array[i];
        }
        return result;
    }

    /**
     * Coverts given floats array to array of bytes.
     *
     * @param array
     *         floats array to convert.
     *
     * @return converted array of bytes.
     */
    public static byte[] toByteArray(float[] array) {
        byte[] result = new byte[array.length];
        for (int i = 0; i < array.length; i++) {
            result[i] = (byte) array[i];
        }
        return result;
    }

    /**
     * Coverts given doubles array to array of bytes.
     *
     * @param array
     *         doubles array to convert.
     *
     * @return converted array of bytes.
     */
    public static byte[] toByteArray(double[] array) {
        byte[] result = new byte[array.length];
        for (int i = 0; i < array.length; i++) {
            result[i] = (byte) array[i];
        }
        return result;
    }

    /**
     * Coverts given bytes array to array of bytes.
     *
     * @param array
     *         bytes array to convert.
     *
     * @return converted array of bytes.
     */
    public static byte[] toByteArray(Byte[] array) {
        byte[] result = new byte[array.length];
        for (int i = 0; i < array.length; i++) {
            result[i] = array[i];
        }
        return result;
    }

    /**
     * Coverts given numbers array to array of bytes.
     *
     * @param array
     *         numbers array to convert.
     *
     * @return converted array of bytes.
     */
    public static byte[] toByteArray(Number[] array) {
        byte[] result = new byte[array.length];
        for (int i = 0; i < array.length; i++) {
            result[i] = array[i].byteValue();
        }
        return result;
    }

    /**
     * Coverts given booleans array to array of bytes.
     *
     * @param array
     *         booleans array to convert.
     *
     * @return converted array of bytes.
     */
    public static byte[] toByteArray(Boolean[] array) {
        byte[] result = new byte[array.length];
        for (int i = 0; i < array.length; i++) {
            result[i] = array[i] ? (byte) 1 : (byte) 0;
        }
        return result;
    }

    /**
     * Coverts given shorts array to array of bytes.
     *
     * @param array
     *         shorts array to convert.
     *
     * @return converted array of bytes.
     */
    public static byte[] toByteArray(Short[] array) {
        byte[] result = new byte[array.length];
        for (int i = 0; i < array.length; i++) {
            result[i] = array[i].byteValue();
        }
        return result;
    }

    /**
     * Coverts given chars array to array of bytes.
     *
     * @param array
     *         chars array to convert.
     *
     * @return converted array of bytes.
     */
    public static byte[] toByteArray(Character[] array) {
        byte[] result = new byte[array.length];
        for (int i = 0; i < array.length; i++) {
            //noinspection UnnecessaryUnboxing
            result[i] = ((byte) array[i].charValue());
        }
        return result;
    }

    /**
     * Coverts given ints array to array of bytes.
     *
     * @param array
     *         ints array to convert.
     *
     * @return converted array of bytes.
     */
    public static byte[] toByteArray(Integer[] array) {
        byte[] result = new byte[array.length];
        for (int i = 0; i < array.length; i++) {
            result[i] = array[i].byteValue();
        }
        return result;
    }

    /**
     * Coverts given longs array to array of bytes.
     *
     * @param array
     *         longs array to convert.
     *
     * @return converted array of bytes.
     */
    public static byte[] toByteArray(Long[] array) {
        byte[] result = new byte[array.length];
        for (int i = 0; i < array.length; i++) {
            result[i] = array[i].byteValue();
        }
        return result;
    }

    /**
     * Coverts given floats array to array of bytes.
     *
     * @param array
     *         floats array to convert.
     *
     * @return converted array of bytes.
     */
    public static byte[] toByteArray(Float[] array) {
        byte[] result = new byte[array.length];
        for (int i = 0; i < array.length; i++) {
            result[i] = array[i].byteValue();
        }
        return result;
    }

    /**
     * Coverts given doubles array to array of bytes.
     *
     * @param array
     *         doubles array to convert.
     *
     * @return converted array of bytes.
     */
    public static byte[] toByteArray(Double[] array) {
        byte[] result = new byte[array.length];
        for (int i = 0; i < array.length; i++) {
            result[i] = array[i].byteValue();
        }
        return result;
    }
}

Related

  1. newByteArray(int size)
  2. newByteArray(int... bytes)
  3. newByteArray(int... bytes)
  4. toByte(char[] bytes, boolean le)
  5. toByteArr(final float value, final boolean lsbIsFirst)
  6. toByteArray(boolean[] bits)
  7. toByteArray(byte arg, int length)
  8. toByteArray(byte b)
  9. toByteArray(byte byteArray)