Java Array Dump dumpAsHex(byte[] src, int length)

Here you can find the source of dumpAsHex(byte[] src, int length)

Description

dump As Hex

License

Open Source License

Declaration

public static String dumpAsHex(byte[] src, int length) 

Method Source Code

//package com.java2s;

public class Main {
    public static String dumpAsHex(byte[] src, int length) {
        StringBuilder out = new StringBuilder(length * 4);
        int p = 0;
        int rows = length / 8;
        for (int i = 0; (i < rows) && (p < length); i++) {
            int ptemp = p;
            for (int j = 0; j < 8; j++) {
                String hexVal = Integer.toHexString(src[ptemp] & 0xff);
                if (hexVal.length() == 1)
                    out.append('0');
                out.append(hexVal).append(' ');
                ptemp++;/*from   ww w .  ja  va  2  s  . c  o  m*/
            }
            out.append("    ");
            for (int j = 0; j < 8; j++) {
                int b = 0xff & src[p];
                if (b > 32 && b < 127) {
                    out.append((char) b).append(' ');
                } else {
                    out.append(". ");
                }
                p++;
            }
            out.append('\n');
        }
        int n = 0;
        for (int i = p; i < length; i++) {
            String hexVal = Integer.toHexString(src[i] & 0xff);
            if (hexVal.length() == 1)
                out.append('0');
            out.append(hexVal).append(' ');
            n++;
        }
        for (int i = n; i < 8; i++) {
            out.append("   ");
        }
        out.append("    ");
        for (int i = p; i < length; i++) {
            int b = 0xff & src[i];
            if (b > 32 && b < 127) {
                out.append((char) b).append(' ');
            } else {
                out.append(". ");
            }
        }
        out.append('\n');
        return out.toString();
    }

    public static String toString(byte[] bytes) {
        if (bytes == null || bytes.length == 0) {
            return "";
        }
        StringBuffer buffer = new StringBuffer();
        for (byte byt : bytes) {
            buffer.append((char) byt);
        }
        return buffer.toString();
    }
}

Related

  1. dumpArray(String msg, Object[] refs)
  2. dumpArray(T[] ts)
  3. dumpArrayHex(byte b[])
  4. dumpAsciiChars(byte[] b, int blen)
  5. dumpAsHex(byte[] byteBuffer, int length)
  6. dumpCharCharArray(String msg, char[][] o)
  7. dumpData(byte data[])
  8. dumpFirst1024Byte(byte[] data)
  9. dumpGroups(String title, int[][] allGroups)