Here you can find the source of dumpAsHex(byte[] byteBuffer, int length)
Parameter | Description |
---|---|
byteBuffer | the data to print as hex |
length | the number of bytes to print |
public static final String dumpAsHex(byte[] byteBuffer, int length)
//package com.java2s; /**// w w w. j av a 2s. c om * <pre> * This program is free software; you can redistribute it and/or modify it under the terms of * the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, * or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU General Public License for more details. * You should have received a copy of the GNU General Public License along with this program; * if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * </pre> */ public class Main { /** * Dumps the given bytes to STDOUT as a hex dump (up to length bytes). * * @param byteBuffer the data to print as hex * @param length the number of bytes to print * @return ... */ public static final String dumpAsHex(byte[] byteBuffer, int length) { StringBuffer outputBuf = new StringBuffer(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(byteBuffer[ptemp] & 0xff); if (hexVal.length() == 1) { hexVal = "0" + hexVal; //$NON-NLS-1$ } outputBuf.append(hexVal + " "); //$NON-NLS-1$ ptemp++; } outputBuf.append(" "); //$NON-NLS-1$ for (int j = 0; j < 8; j++) { int b = 0xff & byteBuffer[p]; if (b > 32 && b < 127) { outputBuf.append((char) b + " "); //$NON-NLS-1$ } else { outputBuf.append(". "); //$NON-NLS-1$ } p++; } outputBuf.append("\n"); //$NON-NLS-1$ } int n = 0; for (int i = p; i < length; i++) { String hexVal = Integer.toHexString(byteBuffer[i] & 0xff); if (hexVal.length() == 1) { hexVal = "0" + hexVal; //$NON-NLS-1$ } outputBuf.append(hexVal + " "); //$NON-NLS-1$ n++; } for (int i = n; i < 8; i++) { outputBuf.append(" "); //$NON-NLS-1$ } outputBuf.append(" "); //$NON-NLS-1$ for (int i = p; i < length; i++) { int b = 0xff & byteBuffer[i]; if (b > 32 && b < 127) { outputBuf.append((char) b + " "); //$NON-NLS-1$ } else { outputBuf.append(". "); //$NON-NLS-1$ } } outputBuf.append("\n"); //$NON-NLS-1$ return outputBuf.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(); } public static String toString(Object[] objs) { if (objs == null || objs.length == 0) return "[]"; StringBuffer buffer = new StringBuffer("["); for (int i = 0; i < objs.length; i++) { buffer.append(String.valueOf(objs[i])); if (i != objs.length - 1) { buffer.append(","); } } buffer.append("]"); return buffer.toString(); } }