Java Hex Calculate toHexString(byte[] buffer)

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

Description

Converts a buffer of bytes into a string of hex values.

License

Open Source License

Parameter

Parameter Description
buffer array of bytes which should be converted

Return

hex string representation of buffer

Declaration

public static String toHexString(byte[] buffer) 

Method Source Code

//package com.java2s;
/**/*from www  .j a v a2s  .  c o m*/
 *
 *  BibSonomy-Common - Common things (e.g., exceptions, enums, utils, etc.)
 *
 *  Copyright (C) 2006 - 2011 Knowledge & Data Engineering Group,
 *                            University of Kassel, Germany
 *                            http://www.kde.cs.uni-kassel.de/
 *
 *  This program 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
 *  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 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, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

public class Main {
    /**
     * Converts a buffer of bytes into a string of hex values.
     * 
     * @param buffer
     *            array of bytes which should be converted
     * @return hex string representation of buffer
     */
    public static String toHexString(byte[] buffer) {
        final StringBuilder result = new StringBuilder();
        for (int i = 0; i < buffer.length; i++) {
            String hex = Integer.toHexString(buffer[i]);
            if (hex.length() == 1) {
                hex = "0" + hex;
            }

            result.append(hex.substring(hex.length() - 2));
        }

        return result.toString();
    }
}

Related

  1. toHexString(byte[] binaryData)
  2. toHexString(byte[] block)
  3. toHexString(byte[] buf)
  4. toHexString(byte[] buf)
  5. toHexString(byte[] buf, int offset, int len)
  6. toHexString(byte[] byteArray)
  7. toHexString(byte[] byteArray)
  8. toHexString(byte[] byteArray)
  9. toHexString(byte[] byteArray)