Java Hex Calculate toHexString(byte bytes[])

Here you can find the source of toHexString(byte bytes[])

Description

Returns an hexadecimal string representation of the given byte array, where each byte is represented by two hexadecimal characters and padded with a zero if its value is comprised between 0 and 15 (inclusive).

License

Open Source License

Parameter

Parameter Description
bytes the array of bytes for which to get an hexadecimal string representation

Return

an hexadecimal string representation of the given byte array

Declaration

public static String toHexString(byte bytes[]) 

Method Source Code

//package com.java2s;
/*//from w  w w . j ava  2  s .co  m
 * This file is part of muCommander, http://www.mucommander.com
 * Copyright (C) 2002-2010 Maxence Bernard
 *
 * muCommander 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 3 of the License, or
 * (at your option) any later version.
 *
 * muCommander 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.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Returns an hexadecimal string representation of the given byte array, where each byte is represented by two
     * hexadecimal characters and padded with a zero if its value is comprised between 0 and 15 (inclusive).
     * As an example, this method will return "6d75636f0a" when called with the byte array {109, 117, 99, 111, 10}.
     *
     * @param bytes the array of bytes for which to get an hexadecimal string representation
     * @return an hexadecimal string representation of the given byte array
     */
    public static String toHexString(byte bytes[]) {
        StringBuilder sb = new StringBuilder();

        for (byte aByte : bytes) {
            String hexByte = Integer.toHexString(aByte & 0xFF);
            if (hexByte.length() == 1)
                sb.append('0');
            sb.append(hexByte);
        }

        return sb.toString();
    }
}

Related

  1. toHexString(byte b[])
  2. toHexString(byte buffer[])
  3. toHexString(byte buffer[])
  4. toHexString(byte bytes[])
  5. toHexString(byte bytes[])
  6. toHexString(byte bytes[])
  7. toHexString(byte bytes[])
  8. toHexString(byte bytes[])
  9. toHexString(byte data[])