Source code: com/anotherbigidea/util/Hex.java
1 /****************************************************************
2 * Copyright (c) 2001, David N. Main, All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or
5 * without modification, are permitted provided that the
6 * following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
10 * disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
16 *
17 * 3. The name of the author may not be used to endorse or
18 * promote products derived from this software without specific
19 * prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
22 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
24 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
32 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 ****************************************************************/
34 package com.anotherbigidea.util;
35
36 import java.io.*;
37
38 /**
39 * Hex dump conversion utilities
40 */
41 public class Hex
42 {
43 public static String dump( byte[] data, long startAddress, String indent )
44 {
45 StringWriter writer = new StringWriter();
46 PrintWriter printer = new PrintWriter( writer, true );
47
48 dump( printer, data, startAddress, indent, false );
49
50 return writer.toString();
51 }
52
53 public static String dumpWithBinary( byte[] data, long startAddress, String indent )
54 {
55 StringWriter writer = new StringWriter();
56 PrintWriter printer = new PrintWriter( writer, true );
57
58 dump( printer, data, startAddress, indent, true );
59
60 return writer.toString();
61 }
62
63 public static void dump( PrintWriter out,
64 byte[] data,
65 long startAddress,
66 String indent,
67 boolean includeBinary )
68 {
69 dump( out, data, 0, data.length, startAddress, indent, includeBinary );
70 }
71
72 public static void dump( PrintWriter out,
73 byte[] data,
74 int startIndex,
75 int length,
76 long startAddress,
77 String indent,
78 boolean includeBinary )
79 {
80 if( data == null ) return; //nothing to dump
81
82 int i = startIndex;
83 int endIndex = startIndex + length - 1;
84 if( endIndex >= data.length ) endIndex = data.length - 1;
85
86 while( i <= endIndex )
87 {
88 String hex = "";
89 String chars = " ";
90 String binary = "";
91
92 for( int j = 0; j < 16; j++ )
93 {
94 if( i + j <= endIndex )
95 {
96 hex += " " + leadingZeros( Integer.toHexString( getByte(data[i+j])), 2 );
97
98 if( includeBinary )
99 {
100 binary += " " + leadingZeros( Integer.toBinaryString( getByte(data[i+j])), 8 );
101 }
102
103 if( data[i+j] < 32 )
104 {
105 chars += ".";
106 }
107 else
108 {
109 chars += new String( new byte[] { data[i+j] } );
110 }
111 }
112 else
113 {
114 hex += " --";
115 chars += " ";
116
117 if( includeBinary )
118 {
119 binary += " --------";
120 }
121 }
122 }
123
124 out.println( ((indent != null) ? indent : "")
125 + leadingZeros( Long.toHexString( startAddress ), 8 )
126 + hex
127 + chars
128 + binary );
129
130 i += 16;
131 startAddress += 16;
132 }
133 }
134
135
136 /**
137 * Pad the string with leading zeros until it it reached the given size
138 */
139 public static String leadingZeros( String string, int size )
140 {
141 String s = string;
142
143 while( s.length() < size )
144 {
145 s = "0" + s;
146 }
147
148 return s;
149 }
150
151 /**
152 * Get unsigned byte
153 */
154 protected static int getByte( byte b )
155 {
156 if( b >= 0 ) return (int)b;
157
158 //else byte is negative and needs conversion to an unsigned int
159 return b + 256;
160 }
161
162 /**
163 * Dump the named file to System.out
164 */
165 public static void main( String[] args ) throws IOException
166 {
167 RandomAccessFile file = new RandomAccessFile( args[0], "r" );
168 byte[] bytes = new byte[ (int)file.length() ];
169 file.readFully( bytes );
170
171 PrintWriter writer = new PrintWriter( System.out );
172 dump( writer, bytes, 0L, "", false );
173 writer.flush();
174 }
175 }