Java Hex Calculate toHexString(byte[] bytes, int offset, int len, int max)

Here you can find the source of toHexString(byte[] bytes, int offset, int len, int max)

Description

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

License

Open Source License

Parameter

Parameter Description
bytes a byte array whose content is to be displayed
offset the offset within the byte array to start at
len the number of bytes
max the maximum number of bytes to be displayed (-1 means no limit)

Return

a String containing the hexadecimal representation of the given bytes

Declaration

public static String toHexString(byte[] bytes, int offset, int len,
        int max) 

Method Source Code

//package com.java2s;
/*/*from   www  .ja v  a  2 s  . c om*/
 *  (c) copyright 2003-2009 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 whose 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.
     *<p>
     * If {@code max} is non-negative and {@code bytes.length > max}, then the
     * first {@code max} bytes are returned, followed by a human-readable
     * indication that there are {@code bytes.length} total bytes of data
     * including those that are not returned.
     * 
     * @param bytes a byte array whose 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.
     * <p>
     * If {@code max} is non-negative and {@code len > max}, then the
     * first {@code max} bytes are returned, followed by a human-readable
     * indication that there are {@code len} total bytes of data
     * including those that are not returned.
     * <p>
     * In particular, {@code offset + len} can extend beyond the array boundaries,
     * as long as {@code offset + max} is still within them, resulting in
     * {@code max} bytes returned followed by an indication that there are
     * {@code len} total data bytes (including those that are not returned).
     *
     * @param bytes a byte array whose content is to be displayed
     * @param offset the offset within the byte array to start at
     * @param len the number of bytes
     * @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) {
        if (bytes == null)
            return "[null]";
        int count = max > -1 && max < len ? max : len;
        StringBuffer s = new StringBuffer();
        for (int i = 0; i < count; i++) {
            String 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)");
        return s.toString();
    }
}

Related

  1. toHexString(byte[] bytes)
  2. toHexString(byte[] bytes)
  3. toHexString(byte[] bytes)
  4. toHexString(byte[] bytes)
  5. toHexString(byte[] bytes, boolean addPrefix)
  6. toHexString(byte[] bytes, String separator, boolean upperCase)
  7. toHexString(byte[] coded)
  8. toHexString(byte[] color)
  9. toHexString(byte[] content, int len)