Java Hex Calculate toHexString(byte[] bytes)

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

Description

Creates a String containing the hexadecimal representation of the given bytes.

License

Open Source License

Parameter

Parameter Description
bytes a byte array who's content is to be displayed

Return

a String containing the hexadecimal representation of the given bytes.

Declaration

public static String toHexString(byte[] bytes) 

Method Source Code

//package com.java2s;
/*//from  ww w.  j  a  va 2s . c  o  m
 *  (c) copyright 2003-2007 Amichai Rothman
 *
 *  This file is part of the Java TNEF package.
 *
 *  The Java TNEF package is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  The Java TNEF package 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 General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

public class Main {
    /**
     * Creates a String containing the hexadecimal representation of the given
     * bytes.
     * 
     * @param bytes
     *            a byte array who's content is to be displayed
     * @return a String containing the hexadecimal representation of the given
     *         bytes.
     */
    public static String toHexString(byte[] bytes) {
        return toHexString(bytes, 0, bytes != null ? bytes.length : 0, -1);
    }

    /**
     * Creates a String containing the hexadecimal representation of the given
     * bytes.
     * 
     * @param bytes
     *            a byte array who's content is to be displayed
     * @param max
     *            the maximum number of bytes to be displayed (-1 means no
     *            limit)
     * @return a String containing the hexadecimal representation of the given
     *         bytes.
     */
    public static String toHexString(byte[] bytes, int max) {
        return toHexString(bytes, 0, bytes != null ? bytes.length : 0, max);
    }

    /**
     * Creates a String containing the hexadecimal representation of the given
     * bytes.
     * 
     * @param bytes
     *            a byte array who's content is to be displayed
     * @param offset
     *            the offset within the byte array to start at
     * @param len
     *            the number of bytes to process
     * @param max
     *            the maximum number of bytes to be displayed (-1 means no
     *            limit)
     * @return a String containing the hexadecimal representation of the given
     *         bytes.
     */
    public static String toHexString(byte[] bytes, int offset, int len,
            int max) {
        int count = max > -1 ? Math.min(max, len) : len;
        StringBuffer s = new StringBuffer();
        s.append('[');
        if (bytes == null) {
            s.append((Object) null);
        } else {
            String b;
            for (int i = 0; i < count; i++) {
                b = Integer.toHexString(bytes[offset + i] & 0xFF)
                        .toUpperCase();
                if (b.length() == 1)
                    s.append('0');
                s.append(b);
            }
            if (count < len) {
                s.append("... (" + len + " bytes)");
            }
        }
        s.append(']');
        return s.toString();
    }
}

Related

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