1 //
2 // Copyright 1998 CDS Networks, Inc., Medford Oregon
3 //
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
8 // 1. Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // 2. Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // 3. All advertising materials mentioning features or use of this software
14 // must display the following acknowledgement:
15 // This product includes software developed by CDS Networks, Inc.
16 // 4. The name of CDS Networks, Inc. may not be used to endorse or promote
17 // products derived from this software without specific prior
18 // written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY CDS NETWORKS, INC. ``AS IS'' AND
21 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 // ARE DISCLAIMED. IN NO EVENT SHALL CDS NETWORKS, INC. BE LIABLE
24 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 // OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 // SUCH DAMAGE.
31 //
32
33
34 package com.internetcds.util;
35
36
37 /***
38 * A simple class to convert a raw buffer to a hex dump
39 *
40 * @version $Id: HexDump.java,v 1.1 2003/04/29 18:07:53 sinisa Exp $
41 * @author Craig Spannring
42 */
43 public class HexDump
44 {
45 public static final String cvsVersion = "$Id: HexDump.java,v 1.1 2003/04/29 18:07:53 sinisa Exp $";
46
47 static String byteToHexString(byte b)
48 {
49 return intToHexString(b, 2, '0');
50 } /* byteToHexString() */
51
52 static String intToHexString(int num, int width, char fill)
53 {
54 String result = "";
55 int i;
56
57 if (num==0)
58 {
59 result = "0";
60 width--;
61 }
62 else
63 {
64 while(num!=0 && width>0)
65 {
66 String tmp = Integer.toHexString(num & 0xf);
67 result = tmp + result;
68 num = (num>>4);
69 width--;
70 }
71 }
72 for(; width>0; width--)
73 {
74 result = fill + result;
75 }
76 return result;
77 } /* intToHexString() */
78
79 public static String hexDump(byte data[])
80 {
81 return hexDump(data, data.length);
82 }
83
84
85 public static String hexDump(byte data[], int length)
86 {
87 String str;
88 int i;
89 int j;
90 final int bytesPerLine = 16;
91 String result = "";
92
93
94 for(i=0; i<length; i+=bytesPerLine)
95 {
96 // print the offset as a 4 digit hex number
97 result = result + intToHexString(i, 4, '0') + " ";
98
99 // print each byte in hex
100 for(j=i; j<length && (j-i)<bytesPerLine; j++)
101 {
102 result = result + byteToHexString(data[j]) + " ";
103 }
104
105 // skip over to the ascii dump column
106 for(; 0!=(j % bytesPerLine); j++)
107 {
108 result = result + " ";
109 }
110 result = result + " |";
111
112 // print each byte in ascii
113 for(j=i; j<length && (j-i)<bytesPerLine; j++)
114 {
115 if (((data[j] & 0xff) > 0x001f) && ((data[j] & 0xff) < 0x007f))
116 {
117 Character ch = new Character((char) data[j]);
118 result = result + ch;
119 }
120 else
121 {
122 result = result + ".";
123 }
124 }
125 result = result + "|\n";
126 }
127 return result;
128 } /* hexDump() */
129 }
This page was automatically generated by Maven