source: other-projects/trunk/gs3-release-maker/tasks/sshtaskdef/src/mindbright/util/HexDump.java@ 14627

Last change on this file since 14627 was 14627, checked in by oranfry, 17 years ago

initial import of the gs3-release-maker

File size: 2.8 KB
Line 
1/******************************************************************************
2 *
3 * Copyright (c) 2000 by Mindbright Technology AB, Stockholm, Sweden.
4 * www.mindbright.se, [email protected]
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 *****************************************************************************
17 * $Author: mats $
18 * $Date: 2000/05/12 12:41:45 $
19 * $Name: rel1-2-1 $
20 *****************************************************************************/
21package mindbright.util;
22
23public class HexDump {
24
25 /* hexadecimal digits. */
26 private static final char[] HEX_DIGITS = {
27 '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
28 };
29
30 /**
31 * Returns a string of 8 hexadecimal digits (most significant
32 * digit first) corresponding to the integer <i>n</i>, which is
33 * treated as unsigned.
34 */
35 public static String intToString (int n) {
36 char[] buf = new char[8];
37 for(int i = 7; i >= 0; i--) {
38 buf[i] = HEX_DIGITS[n & 0x0F];
39 n >>>= 4;
40 }
41 return new String(buf);
42 }
43
44 /**
45 * Returns a string of hexadecimal digits from a byte array. Each
46 * byte is converted to 2 hex symbols.
47 */
48 private static String toString (byte[] ba) {
49 return toString(ba, 0, ba.length);
50 }
51
52 private static String toString (byte[] ba, int offset, int length) {
53 char[] buf = new char[length * 2];
54 for(int i = offset, j = 0, k; i < offset+length; ) {
55 k = ba[i++];
56 buf[j++] = HEX_DIGITS[(k >>> 4) & 0x0F];
57 buf[j++] = HEX_DIGITS[ k & 0x0F];
58 }
59 return new String(buf);
60 }
61
62 public static String formatHex(int i, int sz) {
63 String str = Integer.toHexString(i);
64 while(str.length() < sz) {
65 str = "0" + str;
66 }
67 return str;
68 }
69
70 public static void hexDump(byte[] buf, int off, int len) {
71 int i, j, jmax;
72 int c;
73
74 for(i = 0; i < len; i += 0x10) {
75 String line = formatHex(i + off, 8) + ": ";
76
77 jmax = len - i;
78 jmax = jmax > 16 ? 16 : jmax;
79
80 for(j = 0; j < jmax; j++) {
81 c = ((int)buf[i+j] + 0x100) % 0x100;
82 line += formatHex(c, 2);
83 if ((j % 2) == 1)
84 line += " ";
85 }
86
87 for(; j < 16; j++) {
88 line += " ";
89 if ((j % 2) == 1)
90 line += " ";
91 }
92
93 line += " ";
94
95 for(j = 0; j < jmax; j++) {
96 c = ((int)buf[i+j] + 0x100) % 0x100;
97 c = c < 32 || c >= 127 ? '.' : c;
98 line += (char)c;
99 }
100
101 System.out.println(line);
102 }
103 }
104
105}
Note: See TracBrowser for help on using the repository browser.