Java Hex Calculate toHex(final byte[] bytes, final int offset, final int count)

Here you can find the source of toHex(final byte[] bytes, final int offset, final int count)

Description

Converts the specified array of bytes into a string of hex characters (low byte first).

License

Apache License

Parameter

Parameter Description
bytes the array of <code>byte</code>s that are to be converted. This cannot be <code>null</code> though it may be empty.
offset the offset in <code>bytes</code> at which the bytes will be taken. This cannot be negative and must be less than <code>bytes.length - 1</code>.
count the number of bytes to be retrieved from the specified array. This cannot be negative. If greater than <code>bytes.length - offset</code> then that value is used.

Exception

Parameter Description
IllegalArgumentException if <code>offset</code> is greater thanor equal to <code>bytes.length</code>.

Return

a string of at most count characters that represents the specified byte array in hex. This will never be null though it may be empty if bytes is empty or count is zero.

Declaration

public static String toHex(final byte[] bytes, final int offset, final int count) 

Method Source Code

//package com.java2s;
/*/*from w ww  .j  a  va  2s . c  o  m*/
 * Copyright 2013 Aggregate Knowledge, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    private static final char[] HEX = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E',
            'F' };

    /**
     * Converts the specified array of <code>byte</code>s into a string of
     * hex characters (low <code>byte</code> first).
     *
     * @param  bytes the array of <code>byte</code>s that are to be converted.
     *         This cannot be <code>null</code> though it may be empty.
     * @param  offset the offset in <code>bytes</code> at which the bytes will
     *         be taken.  This cannot be negative and must be less than
     *         <code>bytes.length - 1</code>.
     * @param  count the number of bytes to be retrieved from the specified array.
     *         This cannot be negative.  If greater than <code>bytes.length - offset</code>
     *         then that value is used.
     * @return a string of at most <code>count</code> characters that represents
     *         the specified byte array in hex.  This will never be <code>null</code>
     *         though it may be empty if <code>bytes</code> is empty or <code>count</code>
     *         is zero.
     * @throws IllegalArgumentException if <code>offset</code> is greater than
     *         or equal to <code>bytes.length</code>.
     * @see #fromHex(String, int, int)
     */
    public static String toHex(final byte[] bytes, final int offset, final int count) {
        if (offset >= bytes.length)
            throw new IllegalArgumentException(
                    "Offset is greater than the length (" + offset + " >= " + bytes.length + ").")/*by contract*/;
        final int byteCount = Math.min((bytes.length - offset), count);
        final int upperBound = byteCount + offset;

        final char[] chars = new char[byteCount * 2/*two chars per byte*/];
        int charIndex = 0;
        for (int i = offset; i < upperBound; i++) {
            final byte value = bytes[i];
            chars[charIndex++] = HEX[(value >>> 4) & 0x0F];
            chars[charIndex++] = HEX[value & 0x0F];
        }

        return new String(chars);
    }
}

Related

  1. toHex(final byte[] bytes)
  2. toHex(final byte[] bytes)
  3. toHex(final byte[] bytes)
  4. toHex(final byte[] bytes)
  5. tohex(final byte[] bytes)
  6. toHex(final byte[] data)
  7. toHex(final byte[] data)
  8. toHex(final byte[] data)
  9. toHex(final byte[] data, final int startPos, final int length)