Java Hex Calculate toHexString(byte[] data, int start, int length)

Here you can find the source of toHexString(byte[] data, int start, int length)

Description

toHexString - generate a hex string representation of the specified portion of the given array

License

Open Source License

Parameter

Parameter Description
data The array to be converted
start The starting index within the array
length The number of bytes to represent

Return

The string representation of the given array

Declaration

protected static String toHexString(byte[] data, int start, int length) 

Method Source Code

//package com.java2s;
/*/*ww  w.  j  a v  a 2  s.com*/
 collabREate Utils
 Copyright (C) 2008 Chris Eagle <cseagle at gmail d0t com>
 Copyright (C) 2008 Tim Vidas <tvidas at gmail d0t com>

 This program 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.

 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 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 {
    /**
     * toHexString - generate a hex string representation of the specified
     *               portion of the given array
     * @param data The array to be converted
     * @param start The starting index within the array
     * @param length The number of bytes to represent
     * @return The string representation of the given array
     */
    protected static String toHexString(byte[] data, int start, int length) {
        String hex = "";
        int end = start + length;
        for (int i = start; i < end; i++) {
            //need to ensure that we have a leading zero for bytes < 0x10
            String val = "0" + Integer.toHexString(data[i]);
            hex += val.substring(val.length() - 2);
        }
        return hex;
    }

    /**
     * toHexString - generate a hex string representation of the given array
     * @param data The array to be converted
     * @return The string representation of the given array
     */
    protected static String toHexString(byte[] data) {
        return toHexString(data, 0, data.length);
    }
}

Related

  1. toHexString(byte[] data, final int offset, final int count)
  2. toHexString(byte[] data, int maxLen)
  3. toHexString(byte[] data, int offset, int length)
  4. toHexString(byte[] data, int offset, int length)
  5. toHexString(byte[] data, int start, int len)
  6. toHexString(byte[] digest)
  7. toHexString(byte[] digest)
  8. toHexString(byte[] digest)
  9. toHexString(byte[] digestByte)