Java Byte Array to Hex String bytesToHexString(final byte[] data)

Here you can find the source of bytesToHexString(final byte[] data)

Description

Convert a byte array into a String hex representation.

License

Open Source License

Parameter

Parameter Description
data Input to be converted.

Return

String twice as long as data.length with hex representation of data.

Declaration

public static String bytesToHexString(final byte[] data) 

Method Source Code

//package com.java2s;
/*/*from   w  w w. j a  v a2 s .c o  m*/
 * The MIT License
 *
 * Copyright (c) 2009 The Broad Institute
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

public class Main {
    /**
     * Convert a byte array into a String hex representation.
     * @param data Input to be converted.
     * @return String twice as long as data.length with hex representation of data.
     */
    public static String bytesToHexString(final byte[] data) {
        final char[] chars = new char[2 * data.length];
        for (int i = 0; i < data.length; i++) {
            final byte b = data[i];
            chars[2 * i] = toHexDigit((b >> 4) & 0xF);
            chars[2 * i + 1] = toHexDigit(b & 0xF);
        }
        return new String(chars);
    }

    public static char toHexDigit(final int value) {
        return (char) ((value < 10) ? ('0' + value) : ('A' + value - 10));
    }
}

Related

  1. bytesToHexString(final byte[] bytes)
  2. bytesToHexString(final byte[] bytes)
  3. bytesToHexString(final byte[] bytes)
  4. bytesToHexString(final byte[] bytes)
  5. bytesToHexString(final byte[] bytes, int start, int end)
  6. bytesToHexStringLine(byte[] bs, int lineLength)
  7. bytesToHexStringWithSpace(byte[] bytes)