Java Byte Array Dump dumpBytes(byte[] bytes, int start, int length)

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

Description

Convert a part of an array of bytes, into a string of space-separated hexadecimal numbers.
This method is proposed as a convenience for debugging purposes.

License

Apache License

Parameter

Parameter Description
bytes the array that contains the sequence of byte values to convert.
start the index to start at in the byte array.
length the number of bytes to convert in the array.

Return

the cinverted bytes as a string of space-separated hexadecimal numbers.

Declaration

public static String dumpBytes(byte[] bytes, int start, int length) 

Method Source Code

//package com.java2s;
/*//from  w  w  w  .j a v  a 2 s .co  m
 * JPPF.
 * Copyright (C) 2005-2010 JPPF Team.
 * http://www.jppf.org
 *
 * 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 {
    /**
     * An array of char containing the hex digits in ascending order.
     */
    private static char[] hexDigits = new char[] { '0', '1', '2', '3', '4',
            '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

    /**
     * Convert a part of an array of bytes, into a string of space-separated hexadecimal numbers.<br>
     * This method is proposed as a convenience for debugging purposes.
     * @param bytes the array that contains the sequence of byte values to convert.
     * @param start the index to start at in the byte array.
     * @param length the number of bytes to convert in the array.
     * @return the cinverted bytes as a string of space-separated hexadecimal numbers.
     */
    public static String dumpBytes(byte[] bytes, int start, int length) {
        StringBuilder sb = new StringBuilder();
        if (length >= 0) {
            for (int i = start; i < Math.min(bytes.length, start + length); i++) {
                if (i > start)
                    sb.append(' ');
                sb.append(toHexString(bytes[i]));
            }
        }
        return sb.toString();
    }

    /**
     * Convert a byte value into a 2-digits hexadecimal value. The first digit is 0 if the value is less than 16.<br>
     * If a value is negative, its 2-complement value is converted, otherwise the value itself is converted.
     * @param b the byte value to convert.
     * @return a string containing the 2-digit hexadecimal representation of the byte value.
     */
    public static String toHexString(byte b) {
        int n = (b < 0) ? b + 256 : b;
        StringBuilder sb = new StringBuilder();
        sb.append(hexDigits[n / 16]);
        sb.append(hexDigits[n % 16]);
        return sb.toString();
    }
}

Related

  1. dumpBytes(byte[] buffer)
  2. dumpBytes(byte[] bytes)
  3. dumpBytes(byte[] bytes)
  4. dumpBytes(byte[] bytes)
  5. dumpBytes(byte[] bytes, int maxLen)
  6. dumpBytes(byte[] byts, int offset, int length)
  7. dumpBytes(byte[] data)
  8. dumpBytes(byte[] data)
  9. dumpBytes(final byte[] bytes)