Java Binary to Hex bin2hex(final byte[] b)

Here you can find the source of bin2hex(final byte[] b)

Description

Simple binary-to-hexadecimal conversion.

License

Open Source License

Parameter

Parameter Description
b Input bytes. May be <code>null</code>.

Return

Hexadecimal representation of b. Uppercase A-F, two characters per byte. Empty string on null input.

Declaration

public static String bin2hex(final byte[] b) 

Method Source Code

//package com.java2s;
/*//from  w w  w . ja  va 2 s .  com
 * Used Matthias Gartner's PKCS#5 implementation - see
 * http://rtner.de/software/PBKDF2.html
 * 
 * <p>
 * Free auxiliary functions. Copyright (c) 2007 Matthias G&auml;rtner
 * </p>
 * <p>
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 * </p>
 * <p>
 * This library 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 Lesser General Public License for more
 * details.
 * </p>
 * <p>
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 * </p>
 * <p>
 * For Details, see <a
 * href="http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html"
 * >http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html</a>.
 * </p>
 */

public class Main {
    public static final String hex = "0123456789ABCDEF";

    /**
     * Simple binary-to-hexadecimal conversion.
     * 
     * @param b
     *            Input bytes. May be <code>null</code>.
     * @return Hexadecimal representation of b. Uppercase A-F, two characters
     *         per byte. Empty string on <code>null</code> input.
     */
    public static String bin2hex(final byte[] b) {
        if (b == null) {
            return "";
        }
        StringBuffer sb = new StringBuffer(2 * b.length);
        for (int i = 0; i < b.length; i++) {
            int v = (256 + b[i]) % 256;
            sb.append(hex.charAt((v / 16) & 15));
            sb.append(hex.charAt((v % 16) & 15));
        }
        return sb.toString();
    }
}

Related

  1. bin2hex(byte[] arr)
  2. bin2Hex(byte[] b)
  3. bin2Hex(byte[] bin)
  4. bin2hex(final byte[] base)
  5. bin2hex(int digit)
  6. bin2Hex(String bin)
  7. bin2hex(String bin)