Java Byte Array to Hex bytesToHex(final byte[] bytes, final boolean toLowerCase)

Here you can find the source of bytesToHex(final byte[] bytes, final boolean toLowerCase)

Description

Computes the Hex encoded input.

License

Open Source License

Parameter

Parameter Description
bytes The input bytes to convert to Hex characters
toLowerCase true converts to lowercase or false to uppercase

Return

A String containing hexadecimal characters

Declaration

public static String bytesToHex(final byte[] bytes, final boolean toLowerCase) 

Method Source Code

//package com.java2s;
/*/*  w  ww.  j  a v a  2 s.  c o  m*/
 * ====================
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2008-2009 Sun Microsystems, Inc. All rights reserved.
 *
 * The contents of this file are subject to the terms of the Common Development
 * and Distribution License("CDDL") (the "License").  You may not use this file
 * except in compliance with the License.
 *
 * You can obtain a copy of the License at
 * http://opensource.org/licenses/cddl1.php
 * See the License for the specific language governing permissions and limitations
 * under the License.
 *
 * When distributing the Covered Code, include this CDDL Header Notice in each file
 * and include the License file at http://opensource.org/licenses/cddl1.php.
 * If applicable, add the following below this CDDL Header, with the fields
 * enclosed by brackets [] replaced by your own identifying information:
 * "Portions Copyrighted [year] [name of copyright owner]"
 * ====================
 * Portions Copyrighted 2015 ForgeRock AS.
 */

public class Main {
    final protected static char[] LOWER_HEX_ARRAY = "0123456789abcdef".toCharArray();
    final protected static char[] UPPER_HEX_ARRAY = "0123456789ABCDEF".toCharArray();

    /**
     * Computes the Hex encoded input.
     * 
     * @param bytes
     *            The input bytes to convert to Hex characters
     * @param toLowerCase
     *            {@code true} converts to lowercase or {@code false} to
     *            uppercase
     * @return A String containing hexadecimal characters
     * @since 1.5
     */
    public static String bytesToHex(final byte[] bytes, final boolean toLowerCase) {
        char[] hexChars = new char[bytes.length * 2];
        char[] hexArray = toLowerCase ? LOWER_HEX_ARRAY : UPPER_HEX_ARRAY;
        for (int j = 0; j < bytes.length; j++) {
            int v = bytes[j] & 0xFF;
            hexChars[j * 2] = hexArray[v >>> 4];
            hexChars[j * 2 + 1] = hexArray[v & 0x0F];
        }
        return new String(hexChars);
    }
}

Related

  1. bytesToHex(final byte[] b)
  2. bytesToHex(final byte[] bytes)
  3. bytesToHex(final byte[] bytes)
  4. bytesToHex(final byte[] bytes)
  5. bytesToHex(final byte[] bytes)
  6. bytesToHex(final byte[] bytes, final int position, final int length)
  7. bytesToHex(StringBuffer buff, byte[] src, int start, int end)
  8. bytesToUnsignedHexes(byte[] bytes)
  9. byteToBcd(byte src)