Java Hex String Create appendHexStream(StringBuilder sb, byte[] bytes, String delimiter, boolean prefixEachValue, boolean upperCase)

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

Description

Same as #toHexStream(byte[],String,boolean,boolean) , but appends to an existing StringBuilder.

License

Open Source License

Declaration

public static void appendHexStream(StringBuilder sb, byte[] bytes,
        String delimiter, boolean prefixEachValue, boolean upperCase) 

Method Source Code

//package com.java2s;
/*/*from w w w. j  ava  2  s.com*/
 * 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 {
    /** 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. appendHexEntity(final StringBuilder out, final char value)
  2. appendHexEscape(StringBuilder out, int codePoint)
  3. appendHexJavaScriptRepresentation(StringBuilder sb, char c)
  4. appendHexNumber(StringBuffer target, byte b)
  5. appendHexPair(byte b, StringBuffer sb)
  6. appendHexString(StringBuffer buffer, byte data)
  7. appendHexString(StringBuilder buffer, byte[] bytes)
  8. appendHexString(StringBuilder builder, int value)
  9. appendHexValue(final StringBuffer sb, final byte data)