HexDump

HexDump.java

import java.io.*; 
public class HexDump { 
	public static void main(String[] args) {
		try {
			if (args.length < 1) {
				System.err.println("1 argument is required.");
				System.exit(1);
			} 
			int readLength;
			byte[] buf = new byte[16]; 
			FileInputStream is = new FileInputStream(args[0]); 
			while ((readLength = is.read(buf)) > 0) {
				for (int i=0; i<readLength; i++) {
					int x = buf[i] & 0xFF;
					print(Integer.toHexString(x/16).toUpperCase() + Integer.toHexString(x%16).toUpperCase() + " ");
				}
				for (int i=0; i<16 - readLength; i++)
					print("   ");
				for (int i=0; i<readLength; i++) {
					if (0x20 <= buf[i] && buf[i] <= 0x7E)
						print(new Character((char)buf[i]).toString());
					else
						print(".");
				} 
				print("\n");
			} 
			is.close();
		}
		catch(Exception e) {
			e.printStackTrace();
		}
	}

	public static void print(String s) {
		System.out.print(s);
	}
}

ReverseDump.java

import java.io.FileInputStream; 
import java.io.FileOutputStream;

public class ReverseDump {

	public static void main(String[] args) {
		if (args.length < 1) {
			print("1 argument is required.\n");
			System.exit(1);
		}


		int readLength;
		byte[] buf = new byte[1024];

		try {
			FileOutputStream ofs = new FileOutputStream("rdump.out");

			int numCount = 0;
			StringBuffer bytestr = new StringBuffer();
			FileInputStream is = new FileInputStream(args[0]);
			while ((readLength = is.read(buf)) > 0) {
				for (int i=0; i<readLength; i++) {
					byte x = buf[i];
					if ('a' <= x && x <= 'f')
						x = (byte)(x - 32);
					if (('0' <= x && x <= '9') || ('A' <= x && x <= 'F')) {
						bytestr.append(new Character((char)x));
						if (numCount == 1) {
							numCount = 0;
							byte b = (byte)Integer.parseInt(bytestr.toString(), 16);
							ofs.write(b);
							bytestr.delete(0, bytestr.length());
						} else {
							numCount += 1;
						}
					}
				}
			}

			ofs.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private static void print(String s) {
		System.out.println(s);
	}

}