Java Hex Calculate toHexStream(byte[] bytes, String delimiter, boolean prefixEachValue, boolean upperCase)

Here you can find the source of toHexStream(byte[] bytes, String delimiter, boolean prefixEachValue, boolean upperCase)

Description

Produces a hex stream where bytes are separated by delimiter, and optionally prefixed with "0x" if prefixEachValue is true.

License

Open Source License

Declaration

public static String toHexStream(byte[] bytes, String delimiter,
        boolean prefixEachValue, boolean upperCase) 

Method Source Code

//package com.java2s;
/*//ww w.j  a  v  a2s  .c om
 * TeleStax, Open Source Cloud Communications
 * Copyright 2011-2013, Telestax Inc and individual contributors
 * by the @authors tag.
 *
 * This program is free software: you can redistribute it and/or modify
 * under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation; either version 3 of
 * the License, or (at your option) any later version.
 *
 * This program 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 */

public class Main {
    /** Produces a hex stream where bytes are separated by <code>delimiter</code>, and optionally prefixed with "0x" if <code>prefixEachValue</code> is <code>true</code>.*/
    public static String toHexStream(byte[] bytes, String delimiter,
            boolean prefixEachValue, boolean upperCase) {
        StringBuilder sb = new StringBuilder();
        appendHexStream(sb, bytes, delimiter, prefixEachValue, upperCase);
        return sb.toString();
    }

    /** Same as {@link #toHexStream(byte[], String, boolean, boolean)}, but appends to an existing StringBuilder. */
    public static void appendHexStream(StringBuilder sb, byte[] bytes,
            String delimiter, boolean prefixEachValue, boolean upperCase) {
        sb.ensureCapacity(bytes.length * (prefixEachValue ? 4 : 2)
                + (bytes.length - 1)
                * (delimiter == null ? 0 : delimiter.length()));
        for (int i = 0; i < bytes.length; i++) {
            if (prefixEachValue)
                sb.append("0x");
            char c1 = Character.forDigit((bytes[i] & 0xF0) >> 4, 16);
            char c2 = Character.forDigit((bytes[i] & 0x0F), 16);
            sb.append(upperCase ? Character.toUpperCase(c1) : c1);
            sb.append(upperCase ? Character.toUpperCase(c2) : c2);
            if (delimiter != null && i < bytes.length - 1)
                sb.append(delimiter);
        }
    }
}

Related

  1. toHexPadZero(byte[] bytes)
  2. toHexShortString(byte[] bytes)
  3. toHexStr(byte b[])
  4. toHexStr(byte[] b)
  5. toHexStr(byte[] binary)
  6. toHexString(byte a)
  7. toHexString(byte abyte0[], boolean spaceFlag)
  8. toHexString(byte b)
  9. toHexString(byte b)