Java Byte Array Print printByteWithChar(byte[] byteArray)

Here you can find the source of printByteWithChar(byte[] byteArray)

Description

return beautified String of byte array

License

Apache License

Parameter

Parameter Description
byteArray a parameter

Return

beautified String of byte array

Declaration

public static String printByteWithChar(byte[] byteArray) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**/*from w w w .  ja v  a  2 s.c o  m*/
     * return beautified String of byte array
     * @param byteArray
     * @return beautified String of byte array
     */
    public static String printByteWithChar(byte[] byteArray) {
        int blockSize = 32;

        StringBuilder sb = new StringBuilder();
        StringBuilder sb1 = new StringBuilder();

        for (int i = 0; i < byteArray.length; i++) {
            if (byteArray[i] > 10) {
                //            sb.append(Integer.toHexString(byteArray[i])).append(" ");   
                sb.append(String.format("%02X", byteArray[i])).append(" ");
            } else {
                //            sb.append(" ").append(Integer.toHexString(byteArray[i])).append(" ");
                sb.append(" ").append(String.format("%02X", byteArray[i])).append(" ");
            }

            if ((blockSize - 1) == (i % blockSize)) {
                sb.append("\r\n");
            }
        }

        sb.append("\r\n");

        for (int i = 0; i < byteArray.length; i++) {
            if (byteArray[i] > 10) {
                sb1.append(" ").append((char) byteArray[i]).append(" ");
            } else {
                sb1.append(". ").append(byteArray[i]).append(" ");
            }

            if ((blockSize - 1) == (i % blockSize)) {
                sb1.append("\r\n");
            }
        }

        return sb.toString() + sb1.toString();
    }
}

Related

  1. printBytes(String comment, byte[] input)
  2. printBytes(String x)
  3. printBytesHexEncoded(final byte[] byteArray)
  4. printByteSize(long bytes)
  5. printBytesToString(byte[] bytes, String structName)