blob: 1a79a402647f81679d9acd49cd31c4453ebd3ca9 [file] [log] [blame]
Shuyi Chend7955ce2013-05-22 14:51:55 -07001// Copyright (c) 1999-2004 Brian Wellington (bwelling@xbill.org)
2
3package org.xbill.DNS.utils;
4
5/**
6 * A routine to produce a nice looking hex dump
7 *
8 * @author Brian Wellington
9 */
10
11public class hexdump {
12
13private 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 */
22public static String
23dump(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
51public static String
52dump(String s, byte [] b) {
53 return dump(s, b, 0, b.length);
54}
55
56}