Java Hex Print printHex(byte[] bytes)

Here you can find the source of printHex(byte[] bytes)

Description

Print bytes in Hex format.

License

Apache License

Parameter

Parameter Description
bytes a parameter

Declaration

public static void printHex(byte[] bytes) 

Method Source Code

//package com.java2s;
/*/*from  www  .  j  ava 2s  . com*/
 * Copyright 2016 The Crossing Project
 *
 * The Crossing Project 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 {
    /**
     * Print bytes in Hex format.
     * @param bytes
     */
    public static void printHex(byte[] bytes) {
        if (bytes == null)
            return;
        int lines = bytes.length / 16;
        if (bytes.length % 16 > 0) {
            lines++;
        }
        String readable = new String(bytes);
        int maxlen = readable.length();

        for (int i = 1; i <= lines; i++) {

            for (int j = 0; j < 16; j++) {
                int dot1 = j + (i - 1) * 16;
                if (dot1 < maxlen) {
                    System.out.print(String.format("%2X ", bytes[dot1]));
                }
            }
            System.out.print("    ");

            for (int k = 0; k < 16; k++) {
                int dot2 = k + (i - 1) * 16;
                if (dot2 < maxlen) {
                    System.out.print(String.format("%2c ", readable.charAt(dot2)));
                }
            }

            System.out.println();
        }
    }
}

Related

  1. printHex(byte b)
  2. printHex(byte[] array)
  3. printHex(byte[] array, int offset, int len)
  4. printHex(byte[] b)
  5. printHex(byte[] bytes)
  6. printHex(byte[] data)
  7. printHex(byte[] field, int start, int len)
  8. printHexa(final byte b)