Java Hex Calculate toHexTable(byte[] byteSrc, int lengthOfLine)

Here you can find the source of toHexTable(byte[] byteSrc, int lengthOfLine)

Description

to Hex Table

License

Open Source License

Parameter

Parameter Description
byteSrc a parameter
lengthOfLine a parameter

Declaration

public static String toHexTable(byte[] byteSrc, int lengthOfLine) 

Method Source Code

//package com.java2s;

public class Main {
    /**//from  w ww . j a v  a2s .co  m
     * 
     * 
     * @param byteSrc 
     * @param lengthOfLine 
     * @return 
     */
    public static String toHexTable(byte[] byteSrc, int lengthOfLine) {
        return toHexTable(byteSrc, lengthOfLine, 7);
    }

    /**
     * 
     * 
     * @author 
     * @param byteSrc 
     * @param lengthOfLine 
     * @param column 
     * @return 
     */
    public static String toHexTable(byte[] byteSrc, int lengthOfLine, int column) {
        StringBuffer hexTableBuffer = new StringBuffer(256);
        // int lineCount =
        // (int)Math.ceil((double)byteSrc.length/(double)lengthOfLine);
        int lineCount = byteSrc.length / lengthOfLine;
        int totalLen = byteSrc.length;
        if (byteSrc.length % lengthOfLine != 0)
            lineCount += 1;
        byte[] lineByte;
        for (int lineNumber = 0; lineNumber < lineCount; lineNumber++) {
            int startPos = lineNumber * lengthOfLine;
            lineByte = new byte[Math.min(lengthOfLine, totalLen - startPos)];
            System.arraycopy(byteSrc, startPos, lineByte, 0, lineByte.length);

            int columnA = column & 4;
            if (4 == columnA) {
                int count = 10 * lineNumber;
                String addrStr = Integer.toString(count);
                int len = addrStr.length();
                for (int i = 0; i < 8 - len; i++)
                    hexTableBuffer.append('0');
                // addrStr = fillChar(addrStr, '0', 8, true);
                hexTableBuffer.append(addrStr);
                hexTableBuffer.append("h: ");
            }

            int columnB = column & 2;
            if (2 == columnB) {
                StringBuffer byteStrBuf = new StringBuffer();
                for (int i = 0; i < lineByte.length; i++) {
                    String num = Integer.toHexString(lineByte[i] & 0xff);
                    if (num.length() < 2)
                        byteStrBuf.append('0');
                    byteStrBuf.append(num);
                    byteStrBuf.append(' ');
                    // byteStrBuf.append(fillChar(Integer.toHexString(lineByte[i]&0xff),
                    // '0', 2, true)+ " ");
                }
                // hexTableBuffer.append(byteStrBuf);
                hexTableBuffer.append(fillChar(byteStrBuf.toString(), ' ', 48, false));
                hexTableBuffer.append("; ");
            }

            int columnC = column & 1;
            if (1 == columnC) {
                for (int i = 0; i < lineByte.length; i++) {
                    char c = (char) lineByte[i];
                    if (c < 33)
                        c = '.';
                    try {
                        if (c >= 0xa0 && i < (lineByte.length - 1)) {
                            char c2 = (char) lineByte[i + 1];
                            if (c2 >= 0xa0) {
                                String str = new String(lineByte, i, 2);
                                hexTableBuffer.append(str);
                                i++;
                                continue;
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    hexTableBuffer.append("");
                    hexTableBuffer.append(c);
                }
            }
            if (lineNumber >= lineCount - 1)
                break;
            hexTableBuffer.append('\n');
        }
        return hexTableBuffer.toString();
    }

    /**
     * 
     * @param sSource
     * @param ch
     * @param nLen
     * @param bLeft
     * @return
     */
    public static String fillChar(String sSource, char ch, int nLen, boolean bLeft) {
        int nSrcLen = sSource.length();

        if (nSrcLen <= nLen) { // left fill
            StringBuffer buffer = new StringBuffer();
            if (bLeft) {
                for (int i = 0; i < (nLen - nSrcLen); i++) {
                    buffer.append(ch);
                }
                buffer.append(sSource);
            } else // right fill
            {
                buffer.append(sSource);
                for (int i = 0; i < (nLen - nSrcLen); i++)
                    buffer.append(ch);
            }
            return (buffer.toString());
        }
        return sSource;
    }
}

Related

  1. toHexString2(int i)
  2. toHexString3(byte aByte)
  3. toHexStringBE(char[] array)
  4. toHexStringLE(byte n)
  5. toHexStringUpperCase(byte[] b)
  6. toHexText(byte[] bytes, int length)
  7. toHexUtil(int n)
  8. toHexValue(int number)
  9. toHexWithMarker(byte b)