Java Hex Calculate toHexDump(byte[] bytes)

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

Description

Classname / Method Name : CalculationHelper/toHexDump()

License

Apache License

Parameter

Parameter Description
bytes a parameter

Declaration

public static String toHexDump(byte[] bytes) 

Method Source Code

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

public class Main {
    /**/*from w w  w.j a  v  a2 s.  c o  m*/
     * 
     * Classname / Method Name : CalculationHelper/toHexDump()
     * 
     * @param bytes
     * @return
     * @Description : Method is used to Create HEX dump of input byte array
     */
    public static String toHexDump(byte[] bytes) {

        StringBuffer sb = new StringBuffer();

        for (int i = 0; i < bytes.length; i = i + 16) {
            String hex = Integer.toString(i, 16).toUpperCase();
            if (hex.length() < 4)
                sb.append("0");
            if (hex.length() < 3)
                sb.append("0");
            if (hex.length() < 2)
                sb.append("0");
            sb.append(hex + "\t");

            int j;
            for (j = i; j < i + 16 && j < bytes.length; j++) {

                hex = Integer.toString(bytes[j] & 0xff, 16).toUpperCase();
                if (hex.length() == 1) {
                    hex = "0" + hex;
                }
                sb.append(hex + " ");

            }
            // fill out row
            if (j - i < 16) {
                for (int k = 0; k < 16 - (j - i); k++) {
                    sb.append("   ");
                }
            }

            sb.append("\t");

            for (j = i; j < i + 16 && j < bytes.length; j++) {

                if (bytes[j] < 32 || bytes[j] > 126) {
                    sb.append(".");
                } else {
                    sb.append((char) bytes[j]);
                }
            }

            sb.append("\n");
        }

        return sb.toString();
    }
}

Related

  1. toHexDigit(int value, int digitPosition)
  2. toHexDigits(byte[] bytes)
  3. toHexDigits(int value)
  4. toHexDump(byte[] ab, int cBytesPerLine)
  5. toHexDump(byte[] buffer, int offset, int length, boolean hex, boolean ascii)
  6. toHexEscape(final int u0)
  7. toHexFilter(String inAscii)
  8. toHexFromBin(final String binSymbols)
  9. toHexFromByte(byte b)