Java Byte Array to Hex bytesToHex(final byte[] bytes, final int position, final int length)

Here you can find the source of bytesToHex(final byte[] bytes, final int position, final int length)

Description

Converts an array of bytes starting at position for length bytes to a hexadecimal string.

License

Apache License

Parameter

Parameter Description
bytes the byte array convert into hex
position the byte offset to being converting
length the number of bytes to convert

Return

a hexadecimal string

Declaration

public static final String bytesToHex(final byte[] bytes, final int position, final int length) 

Method Source Code

//package com.java2s;
/*/*from ww w .  j  a  v  a  2s  .c o  m*/
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 = "0123456789abcdef".toCharArray();

    /**
     * Converts an array of bytes starting at <code>position</code> for <code>length</code> bytes
     * to a hexadecimal string.
     *
     * @param bytes the byte array convert into hex
     * @param position the byte offset to being converting
     * @param length the number of bytes to convert
     * @return a hexadecimal string
     */
    public static final String bytesToHex(final byte[] bytes, final int position, final int length) {
        final char[] chars = new char[length * 2];
        int charIndex = 0;
        for (int i = position; i < position + length; i++) {
            chars[charIndex++] = HEX[(bytes[i] & 0xf0) >>> 4];
            chars[charIndex++] = HEX[bytes[i] & 0x0f];
        }
        return String.valueOf(chars);
    }

    /**
     * Converts all the bytes given to a hexadecimal string.
     *
     * @param bytes the bytes to convert to hex
     * @return the hexadecimal string
     */
    public static final String bytesToHex(final byte... bytes) {
        return bytesToHex(bytes, 0, bytes.length);
    }
}

Related

  1. bytesToHex(final byte[] bytes)
  2. bytesToHex(final byte[] bytes)
  3. bytesToHex(final byte[] bytes)
  4. bytesToHex(final byte[] bytes)
  5. bytesToHex(final byte[] bytes, final boolean toLowerCase)
  6. bytesToHex(StringBuffer buff, byte[] src, int start, int end)
  7. bytesToUnsignedHexes(byte[] bytes)
  8. byteToBcd(byte src)
  9. byteToHex(byte b)