Here you can find the source of formatInHex(byte[] bySrc, int nLineLen)
Parameter | Description |
---|---|
bySrc | a parameter |
nLineLen | a parameter |
public static String formatInHex(byte[] bySrc, int nLineLen)
//package com.java2s; public class Main { /**/* w w w . j av a2s . co m*/ * * @param bySrc * @param nLineLen * @return */ public static String formatInHex(byte[] bySrc, int nLineLen) { final String sRepalce = "\n\t\r\0"; int nLength = bySrc.length; int nLine = nLength / nLineLen + 1; String sLineTmp = ""; byte[] byNew = new byte[nLine * nLineLen]; for (int i = 0; i < nLength; i++) byNew[i] = bySrc[i]; String sRet = ""; for (int i = 0; i < nLine; i++) { if (byNew[(i + 1) * nLineLen - 1] < 0) { sLineTmp = new String(byNew, i * nLineLen, nLineLen - 1); sLineTmp += "?"; } else { if (byNew[i * nLineLen] < 0) { sLineTmp = new String(byNew, i * nLineLen + 1, nLineLen - 1); sLineTmp = "?" + sLineTmp; } else sLineTmp = new String(byNew, i * nLineLen, nLineLen); } sLineTmp = " " + sLineTmp; sRet += "[" + fillChar(Integer.toHexString(i * nLineLen + 1), '0', 4, true) + "-" + fillChar(Integer.toHexString(i * nLineLen + nLineLen), '0', 4, true) + "] "; sRet += convertToHex(byNew, i * nLineLen, nLineLen); for (int j = 0, n = sRepalce.length(); j < n; j++) sLineTmp = sLineTmp.replace(sRepalce.charAt(j), '.'); sRet += sLineTmp + "\n"; } return (sRet); } /** * * @param sSource * @param nLineLen * @return */ public static String formatInHex(String sSource, int nLineLen) { final String sRepalce = "\n\t\r\0"; byte[] bySrc = sSource.getBytes(); int nLength = bySrc.length; // System.out.println("nLength = "+nLength); int nLine = nLength / nLineLen + 1; String sLineTmp = ""; byte[] byNew = new byte[nLine * nLineLen]; for (int i = 0; i < nLength; i++) byNew[i] = bySrc[i]; // System.out.println("byNew.nLength = "+byNew.length); String sRet = ""; for (int i = 0; i < nLine; i++) { /* * int k = 0; for( int j=0; j<nLineLen; j++) { if( * byNew[i*nLineLen+j]<0 ) k++; } */ if (byNew[(i + 1) * nLineLen - 1] < 0) { sLineTmp = new String(byNew, i * nLineLen, nLineLen - 1); sLineTmp += "?"; } else { if (byNew[i * nLineLen] < 0) { sLineTmp = new String(byNew, i * nLineLen + 1, nLineLen - 1); sLineTmp = "?" + sLineTmp; } else sLineTmp = new String(byNew, i * nLineLen, nLineLen); } sLineTmp = " " + sLineTmp; sRet += convertToHex(byNew, i * nLineLen, nLineLen); for (int j = 0, n = sRepalce.length(); j < n; j++) sLineTmp = sLineTmp.replace(sRepalce.charAt(j), '.'); sRet += sLineTmp + "\n"; } return (sRet); } /** * * @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; } public static String convertToHex(byte[] bySrc, int offset, int len) { byte[] byNew = new byte[len]; String sTmp = ""; String sResult = ""; for (int i = 0; i < len; i++) byNew[i] = bySrc[offset + i]; for (int i = 0, n = byNew.length; i < n; i++) { sTmp = Integer.toHexString(byNew[i] & 0XFF); sTmp = fillChar(sTmp, '0', 2, true) + " "; sResult += sTmp; } return (sResult); } }