Home | History | Annotate | Download | only in utils
      1 // Copyright (c) 1999-2004 Brian Wellington (bwelling (at) xbill.org)
      2 
      3 package org.xbill.DNS.utils;
      4 
      5 /**
      6  * A routine to produce a nice looking hex dump
      7  *
      8  * @author Brian Wellington
      9  */
     10 
     11 public class hexdump {
     12 
     13 private static final char [] hex = "0123456789ABCDEF".toCharArray();
     14 
     15 /**
     16  * Dumps a byte array into hex format.
     17  * @param description If not null, a description of the data.
     18  * @param b The data to be printed.
     19  * @param offset The start of the data in the array.
     20  * @param length The length of the data in the array.
     21  */
     22 public static String
     23 dump(String description, byte [] b, int offset, int length) {
     24 	StringBuffer sb = new StringBuffer();
     25 
     26 	sb.append(length + "b");
     27 	if (description != null)
     28 		sb.append(" (" + description + ")");
     29 	sb.append(':');
     30 
     31 	int prefixlen = sb.toString().length();
     32 	prefixlen = (prefixlen + 8) & ~ 7;
     33 	sb.append('\t');
     34 
     35 	int perline = (80 - prefixlen) / 3;
     36 	for (int i = 0; i < length; i++) {
     37 		if (i != 0 && i % perline == 0) {
     38 			sb.append('\n');
     39 			for (int j = 0; j < prefixlen / 8 ; j++)
     40 				sb.append('\t');
     41 		}
     42 		int value = (int)(b[i + offset]) & 0xFF;
     43 		sb.append(hex[(value >> 4)]);
     44 		sb.append(hex[(value & 0xF)]);
     45 		sb.append(' ');
     46 	}
     47 	sb.append('\n');
     48 	return sb.toString();
     49 }
     50 
     51 public static String
     52 dump(String s, byte [] b) {
     53 	return dump(s, b, 0, b.length);
     54 }
     55 
     56 }
     57