Java Boolean to Byte booleanToBytes(boolean b)

Here you can find the source of booleanToBytes(boolean b)

Description

This function converts a boolean to its corresponding byte array format.

License

Open Source License

Parameter

Parameter Description
b The boolean to be converted.

Return

The corresponding byte array.

Declaration

static public byte[] booleanToBytes(boolean b) 

Method Source Code

//package com.java2s;
/************************************************************************
 * Copyright (c) Crater Dog Technologies(TM).  All Rights Reserved.     *
 ************************************************************************
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.        *
 *                                                                      *
 * This code is free software; you can redistribute it and/or modify it *
 * under the terms of The MIT License (MIT), as published by the Open   *
 * Source Initiative. (See http://opensource.org/licenses/MIT)          *
 ************************************************************************/

public class Main {
    /**//from   w w w. ja  va  2 s.c  o  m
     * This function converts a boolean to its corresponding byte array format.
     *
     * @param b The boolean to be converted.
     * @return The corresponding byte array.
     */
    static public byte[] booleanToBytes(boolean b) {
        byte[] buffer = new byte[1];
        booleanToBytes(b, buffer);
        return buffer;
    }

    /**
     * This function converts a boolean into its corresponding byte format and inserts
     * it into the specified buffer.
     *
     * @param b The boolean to be converted.
     * @param buffer The byte array.
     * @return The number of bytes inserted.
     */
    static public int booleanToBytes(boolean b, byte[] buffer) {
        return booleanToBytes(b, buffer, 0);
    }

    /**
     * This function converts a boolean into its corresponding byte format and inserts
     * it into the specified buffer at the specified index.
     *
     * @param b The boolean to be converted.
     * @param buffer The byte array.
     * @param index The index in the array to begin inserting bytes.
     * @return The number of bytes inserted.
     */
    static public int booleanToBytes(boolean b, byte[] buffer, int index) {
        int length = 1;
        buffer[index] = (byte) (b ? 0xFF : 0x00);
        return length;
    }
}

Related

  1. booleanToByte(boolean[] array)
  2. booleanToByte(boolean[] bool)
  3. booleanToByte(boolean[] values)
  4. booleanTobyte(final boolean value)
  5. booleanToByteBitflags(boolean[] flags)
  6. booleanToBytes(boolean b)
  7. booleanToBytes(final boolean b)
  8. convertBooleanToByte(boolean inputBool)