Java Formatter Usage byteArrayToHexString(byte[] bytes, int pos, int len)

Here you can find the source of byteArrayToHexString(byte[] bytes, int pos, int len)

Description

Converts a byte array to hex string.

License

Apache License

Parameter

Parameter Description
bytes a byte array.
pos the start position
len number of bytes to be converted

Return

hex string.

Declaration

public static String byteArrayToHexString(byte[] bytes, int pos, int len) 

Method Source Code

//package com.java2s;
/**//from   ww w. j  a v  a2 s.  co 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.
 */

import java.util.Formatter;

public class Main {
    /**
     * Converts a byte array to hex string.
     *
     * @param bytes a byte array.
     * @param pos   the start position
     * @param len   number of bytes to be converted
     * @return hex string.
     */
    public static String byteArrayToHexString(byte[] bytes, int pos, int len) {
        StringBuilder sb = new StringBuilder(bytes.length * 2);
        Formatter formatter = new Formatter(sb);
        for (int i = pos; i < pos + len; ++i) {
            formatter.format("%02x", bytes[i]);
        }
        return sb.toString();
    }
}

Related

  1. byteArray2Hex(byte[] hash)
  2. byteArray2Hex(final byte[] hash)
  3. byteArray2HexString(byte[] buf)
  4. byteArrayToHex(final byte[] hash)
  5. byteArrayToHexString(byte[] bytes)
  6. bytesToHexString(byte[] bytes)
  7. byteToHex(final byte[] bytes)
  8. byteToHexWithPrefix(final byte[] data)
  9. chatting(String fmt, Object... args)