KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ice > util > HexDump


1
2 package com.ice.util;
3
4 import java.lang.*;
5 import java.io.*;
6
7
8 public class
9 HexDump
10     {
11     private static final int ROW_BYTES = 16;
12     private static final int ROW_QTR1 = 3;
13     private static final int ROW_HALF = 7;
14     private static final int ROW_QTR2 = 11;
15
16     public static void
17     dumpHexData( PrintStream out, String JavaDoc title, byte[] buf, int numBytes )
18         {
19         int rows, residue, i, j;
20         byte[] save_buf= new byte[ ROW_BYTES+2 ];
21         byte[] hex_buf = new byte[ 4 ];
22         byte[] idx_buf = new byte[ 8 ];
23         byte[] hex_chars = new byte[20];
24         
25         hex_chars[0] = '0';
26         hex_chars[1] = '1';
27         hex_chars[2] = '2';
28         hex_chars[3] = '3';
29         hex_chars[4] = '4';
30         hex_chars[5] = '5';
31         hex_chars[6] = '6';
32         hex_chars[7] = '7';
33         hex_chars[8] = '8';
34         hex_chars[9] = '9';
35         hex_chars[10] = 'A';
36         hex_chars[11] = 'B';
37         hex_chars[12] = 'C';
38         hex_chars[13] = 'D';
39         hex_chars[14] = 'E';
40         hex_chars[15] = 'F';
41         
42         out.println( title + " - " + numBytes + " bytes." );
43         rows = numBytes >> 4;
44         residue = numBytes & 0x0000000F;
45         for ( i = 0 ; i < rows ; i++ )
46             {
47             int hexVal = (i * ROW_BYTES);
48             idx_buf[0] = hex_chars[((hexVal >> 12) & 15)];
49             idx_buf[1] = hex_chars[((hexVal >> 8) & 15)];
50             idx_buf[2] = hex_chars[((hexVal >> 4) & 15)];
51             idx_buf[3] = hex_chars[(hexVal & 15)];
52
53             String JavaDoc idxStr = new String JavaDoc( idx_buf, 0, 4 );
54             out.print( idxStr + ": " );
55         
56             for ( j = 0 ; j < ROW_BYTES ; j++ )
57                 {
58                 save_buf[j] = buf[(i * ROW_BYTES) + j];
59
60                 hex_buf[0] = hex_chars[(save_buf[j] >> 4) & 0x0F];
61                 hex_buf[1] = hex_chars[save_buf[j] & 0x0F];
62
63                 out.print( (char)hex_buf[0] );
64                 out.print( (char)hex_buf[1] );
65                 out.print( ' ' );
66
67                 if ( j == ROW_QTR1 || j == ROW_HALF || j == ROW_QTR2 )
68                     out.print( " " );
69
70                 if (save_buf[j] < 0x20 || save_buf[j] > 0xD9)
71                     save_buf[j] = '.';
72                 }
73
74             String JavaDoc saveStr = new String JavaDoc( save_buf, 0, j );
75             out.println( " | " + saveStr + " |" );
76             }
77         
78         if ( residue > 0 )
79             {
80             int hexVal = (i * ROW_BYTES);
81             idx_buf[0] = hex_chars[((hexVal >> 12) & 15)];
82             idx_buf[1] = hex_chars[((hexVal >> 8) & 15)];
83             idx_buf[2] = hex_chars[((hexVal >> 4) & 15)];
84             idx_buf[3] = hex_chars[(hexVal & 15)];
85
86             String JavaDoc idxStr = new String JavaDoc( idx_buf, 0, 4 );
87             out.print( idxStr + ": " );
88
89             for ( j = 0 ; j < residue ; j++ )
90                 {
91                 save_buf[j] = buf[(i * ROW_BYTES) + j];
92
93                 hex_buf[0] = hex_chars[(save_buf[j] >> 4) & 0x0F];
94                 hex_buf[1] = hex_chars[save_buf[j] & 0x0F];
95
96                 out.print( (char)hex_buf[0] );
97                 out.print( (char)hex_buf[1] );
98                 out.print( ' ' );
99
100                 if ( j == ROW_QTR1 || j == ROW_HALF || j == ROW_QTR2 )
101                     out.print( " " );
102
103                 if (save_buf[j] < 0x20 || save_buf[j] > 0xD9)
104                     save_buf[j] = '.';
105                 }
106                 
107             for ( /*j INHERITED*/ ; j < ROW_BYTES ; j++)
108                 {
109                 save_buf[j] = ' ';
110                 out.print( " " );
111                 if ( j == ROW_QTR1 || j == ROW_HALF || j == ROW_QTR2 )
112                     out.print( " " );
113                 }
114             
115             String JavaDoc saveStr = new String JavaDoc( save_buf, 0, j );
116             out.println( " | " + saveStr + " |" );
117             }
118         }
119
120     static public void
121     main( String JavaDoc[] args )
122         {
123         byte[] data = new byte[132];
124         for ( int i = 0 ; i < 132 ; ++i ) data[i] = (byte)i;
125
126         HexDump.dumpHexData( System.err, "Test HexDump", data, 132 );
127         }
128
129     }
130
131
132
133
Popular Tags