Java Hex Calculate toHex(boolean[] bits)

Here you can find the source of toHex(boolean[] bits)

Description

Returns the hexadecimal number of a bit array .

License

Open Source License

Parameter

Parameter Description
bits the boolean array representing the bits to be converted into hexadecimal numbers

Return

the hexadecimal number

Declaration

public static final String toHex(boolean[] bits) 

Method Source Code

//package com.java2s;
/**/* w  w w . j a  v a 2  s  .c o m*/
 * Copyright (c) 2010-2016, openHAB.org and others.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */

public class Main {
    /**
     * Returns the hexadecimal number of a number of integer values.
     * 
     * @param values
     *            the integer values to be converted into hexadecimal numbers
     * @return the given numbers as hexadecimal number
     */
    public static final String toHex(int... values) {
        String returnValue = "";
        for (int v : values) {
            returnValue += v < 16 ? "0"
                    + Integer.toHexString(v).toUpperCase() : Integer
                    .toHexString(v).toUpperCase();
        }
        return returnValue;
    }

    /**
     * Returns the hexadecimal number of a bit array .
     * 
     * @param bits
     *            the boolean array representing the bits to be converted into hexadecimal numbers
     * @return the hexadecimal number
     */
    public static final String toHex(boolean[] bits) {
        int retVal = 0;
        for (int i = 0; i < bits.length; ++i) {
            retVal |= (bits[i] ? 1 : 0) << i;
        }
        return toHex(retVal);
    }
}

Related

  1. toHex(byte b)
  2. toHex(byte b)
  3. toHex(byte b)
  4. tohex(byte b)