jump to navigation

Volcado hexadecimal abril 11, 2007

Posted by superpiwi in Java, Programacion.
trackback

En David Tribble’s Java Source Code me encuentro la siguiente clase Java que nos sirve para mostrar una traza hexadecimal de un String o el contenido de un fichero. (Por cierto hay mucho e interesante codigo de ejemplo en ese website).

Para usarla un ejemplo sencillo:


String TEXTO="Si durante el combate os veis caminando por verdes praderas, bañadas por la suave brisa del viento, que no os cause temor, porque estaréis en el Eliseo... ¡Y ya habréis muerto!...\r\nHermanos lo que hacemos en esta vida tiene su eco en la eternidad";
hexDump(System.out, TEXTO);

que nos muestra:

traza.jpg

Usala si te viene bien, a mi me vendra estupendamente para mostrar el contenido de los ficheros ya codificados en ASN1.

Se llama “HexDump”, aqui lo que interesa:


//==============================================================================
// HexDump.java
//------------------------------------------------------------------------------
// System imports
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
//import java.io.FileReader;
import java.io.InputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.lang.NullPointerException;
import java.lang.String;
import java.lang.System;
// Local imports
// (None)
/*******************************************************************************
* Hexadecimal dump utility methods.
*
* @version $Revision: 1.4 $ $Date: 2003/02/18 00:33:11 $
* @since 2000-02-08
* @author
* David R. Tribble,
* <a href="mailto:david@tribble.com">david@tribble.com</a>.
* <br>
* Copyright ©2000-2003 by David R. Tribble, all rights reserved.
* <br>
* Permission is granted to freely use and distribute this source code
* provided that the original copyright and authorship notices remain
* intact.
*
* @see HexDumper
*/
public abstract class HexDump
{
// Identification
/** Class version number. */
public static final int VERS = 100;
/** Revision info. */
static final String REV =
"@(#)tribble/util/HexDump.java $Revision: 1.4 $ $Date: 2003/02/18 00:33:11 $\n";
/** Copyright info. */
static final String COPYRIGHT =
"Copyright ©2000-2003 by David R. Tribble, all rights reserved.";
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Constants
// (None)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Static class initializers
/*+++TEST CODE, NO LONGER USED
// Test vectors for hexDumpString()
static
{
String s;
hexDump(System.out,
"Hello, world\n................................."
+ "xxxxxxxx\u202Bxx\uFFFFxxxx/"
+ "4%%%%\t%%%%%%%%%%4%%%%%%%%%%%%%%%%4%%%%%%%%%%%%4%%%%%%%%%%%%4[]"
+ "5%%%%%%%%%%%%%%%5%%%%%%%%%%%%%%%%5%%%%%%%%%%%%5%%%%%%%%%%%%5[]"
+ "6%%%%%%%%%%%%%%%6%%%%%%%%%%%%%%%%6%%%%%%%%%%%%6%%%%%%%%%%%%6[]");
System.out.println();
hexDump(System.out, "Hello, world\n");
System.out.println();
hexDump(System.out, "abuuuuuuuukkkkkkkkk");
System.out.println();
hexDump(System.out, "abuuuuuuuukkkkkkkkkk");
System.out.println();
hexDump(System.out, "abuuuuuuuukkkkkkkkkkx");
System.out.println();
s = "abuuuuuuuukkkkkkkkkkx";
hexDump(System.out, s, 0, 0);
System.out.println();
hexDump(System.out, s, 0, 4);
System.out.println();
hexDump(System.out, s, 0, s.length()+100);
System.out.println();
hexDump(System.out, s, 1, s.length());
System.out.println();
hexDump(System.out, s, 100, 4);
System.out.println();
}
+++*/
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Public static methods
/***************************************************************************
* Print the contents of a byte array as a hexadecimal dump to an output
* stream.
*
* <p>
* This method is equivalent to:
* <pre> hexDump(out, data, 0, data.length); </pre>
*
* @param out
* An output stream.
*
* @param data
* An array of bytes to dump to <tt>out</tt>.
*
* @since 1.3, 2003-02-15
*/
public static void hexDump(PrintWriter out, byte[] data)
{
// Dump the characters in 'data'
hexDump(out, data, 0, data.length);
}
/***************************************************************************
* Print the contents of a byte array as a hexadecimal dump to an output
* stream.
*
* @param out
* An output stream.
*
* @param data
* An array of bytes to dump to <tt>out</tt>.
*
* @param off
* The index of the first byte within the byte array to dump.
*
* @param len
* The number of bytes within the byte array to dump.
*
* @since 1.3, 2003-02-15
*/
public static void hexDump(PrintWriter out, byte[] data, int off, int len)
{
// Check args
if (out == null)
return;
if (data == null)
{
out.println("Null");
return;
}
// Dump the bytes in 'data'
hexDumpAt(out, data, off, len, 0);
out.flush();
}
/***************************************************************************
* Print the contents of a byte array as a hexadecimal dump to an output
* stream.
*
* <p>
* This method is equivalent to:
* <pre> hexDump(out, data, 0, data.length); </pre>
*
* @param out
* An output stream.
*
* @param data
* An array of bytes to dump to <tt>out</tt>.
*
* @since 1.3, 2003-02-15
*/
public static void hexDump(PrintStream out, byte[] data)
{
// Dump the bytes in 'data'
hexDump(new PrintWriter(out, false), data, 0, data.length);
}
/***************************************************************************
* Print the contents of a byte array as a hexadecimal dump to an output
* stream.
*
* @param out
* An output stream.
*
* @param data
* An array of bytes to dump to <tt>out</tt>.
*
* @param off
* The index of the first byte within the byte array to dump.
*
* @param len
* The number of bytes within the byte array to dump.
*
* @since 1.3, 2003-02-15
*/
public static void hexDump(PrintStream out, byte[] data, int off, int len)
{
// Dump the bytes in 'data'
hexDump(new PrintWriter(out, false), data, off, len);
}
/***************************************************************************
* Print the contents of a string as a hexadecimal dump to an output stream.
*
* <p>
* This method is equivalent to:
* <pre> hexDump(out, s, 0, s.length()); </pre>
*
* @param out
* An output stream.
*
* @param s
* A string of Unicode characters to dump to <tt>out</tt>.
*
* @since 1.3, 2003-02-15
*/
public static void hexDump(PrintWriter out, String s)
{
// Dump the characters in 's'
hexDump(out, s, 0, s.length());
}
/***************************************************************************
* Print the contents of a string as a hexadecimal dump to an output stream.
*
* @param out
* An output stream.
*
* @param off
* The index of the first character within the string to dump.
*
* @param len
* The number of bytes within the byte array to dump.
*
* @param s
* A string of Unicode characters to dump to <tt>out</tt>.
*
* @since 1.3, 2003-02-15
*/
public static void hexDump(PrintWriter out, String s, int off, int len)
{
// Check args
if (out == null)
return;
if (s == null)
{
out.println("Null");
return;
}
// Dump the characters in 's'
hexDumpStringAt(out, s, off, len, 0);
out.flush();
}
/***************************************************************************
* Print the contents of a string as a hexadecimal dump to an output stream.
*
* <p>
* This method is equivalent to:
* <pre> hexDump(out, s, 0, s.length()); </pre>
*
* @param out
* An output stream.
*
* @param s
* A string of Unicode characters to dump to <tt>out</tt>.
*
* @since 1.3, 2003-02-15
*/
public static void hexDump(PrintStream out, String s)
{
// Dump the characters in 's'
hexDump(new PrintWriter(out, false), s, 0, s.length());
}
/***************************************************************************
* Print the contents of a string as a hexadecimal dump to an output stream.
*
* @param out
* An output stream.
*
* @param off
* The index of the first character within the string to dump.
*
* @param len
* The number of bytes within the byte array to dump.
*
* @param s
* A string of Unicode characters to dump to <tt>out</tt>.
*
* @since 1.3, 2003-02-15
*/
public static void hexDump(PrintStream out, String s, int off, int len)
{
hexDump(new PrintWriter(out, false), s, off, len);
}
/***************************************************************************
* Print the contents of an input stream as a hexadecimal dump to an output
* stream.
*
* <p>
* Note that any {@link java.io.IOException}s thrown while reading from the
* input stream or writing to the output stream are ignored.
*
* @param in
* An input (byte) stream.
* Note that this stream is <i>not</i> closed after reading its contents.
*
* @param out
* An output stream.
*
* @since 1.3, 2003-02-15
*/
public static void hexDump(InputStream in, PrintWriter out)
{
// Check args
if (out == null)
return;
if (in == null)
throw new NullPointerException("Null input stream");
// Read and dump the contents of the input stream
try
{
byte[] data; // Input data block
int off; // Leftover data bytes
int addr; // Data address
// Read and dump the contents of the input stream
data = new byte[64*1024];
off = 0;
addr = 0;
for (;;)
{
int len;
int dlen;
// Read a block of data from the file
len = in.read(data, off, data.length - off);
if (len <= 0)
len = 0;
// Dump the data block in hexadecimal
dlen = (off+len) & ~0x0000000F; // = (off+len)/16 * 16
hexDumpAt(out, data, 0, dlen, addr);
addr += dlen;
// Save any leftover bytes for the next iteration
off = off+len - dlen;
if (off > 0)
System.arraycopy(data, dlen, data, 0, off);
// Check for end of file
if (len == 0)
{
// End of file reached, dump the last remaining data bytes
hexDumpAt(out, data, 0, off, addr);
addr += off;
// Stop reading and dumping
break;
}
}
out.flush();
}
catch (IOException ex)
{
// Ignore
}
}
/***************************************************************************
* Print the contents of an input stream as a hexadecimal dump to an output
* stream.
*
* <p>
* Note that any {@link java.io.IOException}s thrown while reading from the
* input stream or writing to the output stream are ignored.
*
* @param in
* An input (byte) stream.
* Note that this stream is <i>not</i> closed after reading its contents.
*
* @param out
* An output stream.
*
* @since 1.3, 2003-02-15
*/
public static void hexDump(InputStream in, PrintStream out)
{
hexDump(in, new PrintWriter(out, false));
}
/***************************************************************************
* Print the contents of a byte array as a hexadecimal dump to an output
* stream.
*
* @param out
* An output stream.
*
* @param data
* An array of bytes to dump to <tt>out</tt>.
*
* @param off
* The index of the first byte within the byte array to dump.
*
* @param len
* The number of bytes within the byte array to dump
*
* @param base
* The base address to assume in the printed dump.
*
* @since 1.4, 2003-02-17 (2003-02-15)
*/
public static void hexDumpAt(PrintWriter out, byte[] data,
int off, int len, int base)
{
int loc;
int end;
// Print a hexadecimal dump of 'data[off...off+len-1]'
if (off >= data.length)
off = data.length;
end = off+len;
if (end >= data.length)
end = data.length;
len = end-off;
if (len <= 0)
return;
loc = (off/0x10)*0x10;
for (int i = loc; i < end; i += 0x10, loc += 0x10)
{
int j;
// Print the location/offset
{
int v;
v = base+loc;
for (j = (8-1)*4; j >= 0; j -= 4)
{
int d;
d = (v >>> j) & 0x0F;
d = (d < 0xA ? d+'0' : d-0xA+'A');
out.print((char) d);
}
}
// Print a row of hex bytes
out.print(" ");
for (j = 0x00; i+j < off; j++)
out.print(".. ");
for ( ; j < 0x10 && i+j < end; j++)
{
int ch;
int d;
if (j == 0x08)
out.print(' ');
ch = data[i+j] & 0xFF;
d = (ch >>> 4);
d = (d < 0xA ? d+'0' : d-0xA+'A');
out.print((char) d);
d = (ch & 0x0F);
d = (d < 0xA ? d+'0' : d-0xA+'A');
out.print((char) d);
out.print(' ');
}
for ( ; j < 0x10; j++)
{
if (j == 0x08)
out.print(' ');
out.print(".. ");
}
// Print a row of printable characters
out.print(" |");
for (j = 0x00; i+j < off; j++)
out.print(' ');
for ( ; j < 0x10 && i+j < end; j++)
{
int ch;
ch = data[i+j] & 0xFF;
if (ch < 0x20 ||
ch >= 0x7F && ch < 0xA0 ||
ch > 0xFF)
{
// The character is unprintable
ch = '.';
}
out.print((char) ch);
}
for ( ; j < 0x10; j++)
out.print(' ');
out.println("|");
}
}
/***************************************************************************
* Print the contents of a string as a hexadecimal dump to an output stream.
*
* @param out
* An output stream.
*
* @param data
* A string of Unicode characters to dump to <tt>out</tt>.
*
* @param off
* The index of the first byte within the byte array to dump.
*
* @param len
* The number of bytes within the byte array to dump
*
* @param base
* The base address to assume in the printed dump.
*
* @since 1.4, 2003-02-17 (2003-02-15)
*/
public static void hexDumpStringAt(PrintWriter out, String data,
int off, int len, int base)
{
int loc;
int end;
char[] h;
// Print a hexadecimal dump of 'data[off...off+len-1]'
if (off >= data.length())
off = data.length();
end = off+len;
if (end >= data.length())
end = data.length();
len = end-off;
if (len <= 0)
return;
loc = (off/0x10)*0x10;
h = new char[10];
for (int i = loc; i < end; i += 10, loc += 10)
{
int j;
// Print the location/offset
{
int v;
v = base+loc;
for (j = 1; j <= 10; j++)
{
int d;
d = (v % 10) + '0';
v = (v / 10);
h[10-j] = (char) d;
}
// Print the location/offset as a 4-10 digit decimal number
for (j = 0; j < 10-1; j++)
{
if (h[j] == '0')
h[j] = ' ';
else
break;
}
for (j = 0; j < 6; j++)
if (h[j] != ' ')
break;
for ( ; j < 10; j++)
out.print(h[j]);
}
// Print a row of hex bytes
out.print(": ");
for (j = 0; i+j < off; j++)
out.print(".... ");
for ( ; j < 10 && i+j < end; j++)
{
int ch;
int d;
ch = (int) data.charAt(i+j);
d = (ch >>> 12) & 0x0F;
d = (d < 0xA ? d+'0' : d-0xA+'A');
out.print((char) d);
d = (ch >>> 8) & 0x0F;
d = (d < 0xA ? d+'0' : d-0xA+'A');
out.print((char) d);
d = (ch >>> 4) & 0x0F;
d = (d < 0xA ? d+'0' : d-0xA+'A');
out.print((char) d);
d = (ch & 0x0F);
d = (d < 0xA ? d+'0' : d-0xA+'A');
out.print((char) d);
out.print(' ');
}
for ( ; j < 10; j++)
out.print(".... ");
// Print a row of printable characters
out.print('|');
for (j = 0; i+j < off; j++)
out.print(' ');
for ( ; j < 10 && i+j < end; j++)
{
int ch;
ch = (int) data.charAt(i+j);
if (ch < 0x0020 ||
ch >= 0x007F && ch < 0x00A0 ||
ch > 0x00FF)
{
// Assume that the character is unprintable
ch = '.';
}
out.print((char) ch);
}
for ( ; j < 10; j++)
out.print(' ');
out.println("|");
}
}
/***************************************************************************
* Print the contents of one or more files as a hexadecimal dump to the
* standard output stream.
*
* <!-------------------------------------------------------------------- -->
* <p>
* <b> Usage </b>
* <pre>
* java tribble.util.HexDump <i>file...</i> </pre>
*
* @param args
* Command line arguments (filenames to dump).
*
* @since 1.1, 2000-02-08
*/
public static void main(String[] args)
{
String s;
/*hexDump(System.out,
"Hello, world\n................................."
+ "xxxxxxxx\u202Bxx\uFFFFxxxx/"
+ "4%%%%\t%%%%%%%%%%4%%%%%%%%%%%%%%%%4%%%%%%%%%%%%4%%%%%%%%%%%%4[]"
+ "5%%%%%%%%%%%%%%%5%%%%%%%%%%%%%%%%5%%%%%%%%%%%%5%%%%%%%%%%%%5[]"
+ "6%%%%%%%%%%%%%%%6%%%%%%%%%%%%%%%%6%%%%%%%%%%%%6%%%%%%%%%%%%6[]");
*/
System.out.println();
String TEXTO="Si durante el combate os veis caminando por verdes praderas, bañadas por la suave brisa del viento, que no os cause temor, porque estaréis en el Eliseo... ¡Y ya habréis muerto!...\r\nHermanos lo que hacemos en esta vida tiene su eco en la eternidad";
hexDump(System.out, TEXTO);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Variables
// (None)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Constructors
/***************************************************************************
* Default constructor.
*
* @since 1.1, 2000-02-08
*/
private HexDump()
{
// Do nothing
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Methods
// (None)
}
// End HexDump.java

Comentarios»

No comments yet — be the first.

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s

A %d blogueros les gusta esto: