1 33 34 package com.internetcds.util; 35 36 37 43 public class HexDump 44 { 45 public static final String cvsVersion = "$Id: HexDump.java,v 1.1 2006/06/23 10:39:30 sinisa Exp $"; 46 47 static String byteToHexString(byte b) 48 { 49 return intToHexString(b, 2, '0'); 50 } 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 } 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 result = result + intToHexString(i, 4, '0') + " "; 98 99 for(j=i; j<length && (j-i)<bytesPerLine; j++) 101 { 102 result = result + byteToHexString(data[j]) + " "; 103 } 104 105 for(; 0!=(j % bytesPerLine); j++) 107 { 108 result = result + " "; 109 } 110 result = result + " |"; 111 112 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 } 129 } 130 | Popular Tags |