Here you can find the source of toHexStream(byte[] bytes, String delimiter, boolean prefixEachValue, boolean upperCase)
delimiter
, and optionally prefixed with "0x" if prefixEachValue
is true
.
public static String toHexStream(byte[] bytes, String delimiter, boolean prefixEachValue, boolean upperCase)
//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); } } }